简体   繁体   中英

nl2br not working with BBCode

I had data in my table like this:

#inlcude<stdio.h>
main() {
printf("Hi");
}

But when I retrieve data from the database, it displays:

#inlude<stdio.h>main() { printf("Hi"); }

As you can see, there are no line breaks. Here is my entire code:

function BBCode ($str) {  
$str = htmlentities($str);  

$simple_search = array(  
            //added line break  
            '/\[br\]/is',  
            '/\[b\](.*?)\[\/b\]/is',  
            '/\[i\](.*?)\[\/i\]/is',  
            '/\[u\](.*?)\[\/u\]/is',  
            '/\[url\=(.*?)\](.*?)\[\/url\]/is',  
            '/\[url\](.*?)\[\/url\]/is',  
            '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
            '/\[img\](.*?)\[\/img\]/is',  
            '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
            '/\[mail\](.*?)\[\/mail\]/is',  
            '/\[font\=(.*?)\](.*?)\[\/font\]/is',  
            '/\[size\=(.*?)\](.*?)\[\/size\]/is',  
            '/\[color\=(.*?)\](.*?)\[\/color\]/is',  
              //added textarea for code presentation  
           '/\[codearea\](.*?)\[\/codearea\]/is',             
            //added paragraph  
          '/\[p\](.*?)\[\/p\]/is',            
           //smilies
           '/\:angry:/is',
            '/\:angel:/is',
             '/\:arrow:/is',
              '/\:at:/is',
               '/\:biggrin:/is',
                '/\:blank:/is',
                 '/\:blush:/is',
                  '/\:confused:/is',
                   '/\:cool:/is',
                    '/\:dodgy:/is',
                     '/\:exclamation:/is',
                      '/\:heart:/is',
                       '/\:huh:/is',
                        '/\:lightbulb:/is',
                         '/\:my:/is',
                          '/\:rolleyes:/is',
                           '/\:sad:/is',
                            '/\:shy:/is',
                             '/\:sleepy:/is',
                              '/\:smile:/is',
                               '/\:tongue:/is',
                                '/\:undecided:/is',
                                '/\:wink:/is',


                                // For 3rd Party Code
                                '/\[code\](.*?)\[\/code\]/is', 
                                 '/\[php\](.*?)\[\/php\]/is',  
                                  '/\[css\](.*?)\[\/css\]/is', 
                                    '/\[jscript\](.*?)\[\/jscript\]/is', 
                                     '/\[sql\](.*?)\[\/sql\]/is', 
                                      '/\[perl\](.*?)\[\/perl\]/is', 


            );  

$simple_replace = array(  
            //added line break  
            '<br />',  
            '<strong>$1</strong>',  
            '<em>$1</em>',  
            '<u>$1</u>',  
            // added nofollow to prevent spam  
            '<a href="$1" rel="nofollow" title="$2 - $1">$2</a>',  
            '<a href="$1" rel="nofollow" title="$1">$1</a>',  
            '<div style="text-align: $1;">$2</div>',  
            //added alt attribute for validation  
            '<img src="$1" alt="" />',  
            '<a href="mailto:$1">$2</a>',  
            '<a href="mailto:$1">$1</a>',  
            '<span style="font-family: $1;">$2</span>',  
            '<span style="font-size: $1;">$2</span>',  
            '<span style="color: $1;">$2</span>',  
            //added textarea for code presentation  
            '<textarea class="code_container" rows="20" cols="65">$1</textarea>',  
            //added paragraph  
            '<p>$1</p>',  
            //smilies
            '<img src="comments/smilies/angry.gif" border="0" alt="" />',
            '<img src="comments/smilies/angel.gif" border="0" alt="" />',
            '<img src="comments/smilies/arrow.gif" border="0" alt="" />',
            '<img src="comments/smilies/at.gif" border="0" alt="" />',
            '<img src="comments/smilies/biggrin.gif" border="0" alt="" />',
            '<img src="comments/smilies/blank.gif" border="0" alt="" />',
            '<img src="comments/smilies/blush.gif" border="0" alt="" />',
            '<img src="comments/smilies/confused.gif" border="0" alt="" />',
            '<img src="comments/smilies/cool.gif" border="0" alt="" />',
            '<img src="comments/smilies/dodgy.gif" border="0" alt="" />',
            '<img src="comments/smilies/exclamation.gif" border="0" alt="" />',
            '<img src="comments/smilies/heart.gif" border="0" alt="" />',
            '<img src="comments/smilies/huh.gif" border="0" alt="" />',
            '<img src="comments/smilies/lightbulb.gif" border="0" alt="" />',
            '<img src="comments/smilies/my.gif" border="0" alt="" />',
            '<img src="comments/smilies/rolleyes.gif" border="0" alt="" />',
            '<img src="comments/smilies/sad.gif" border="0" alt="" />',
            '<img src="comments/smilies/shy.gif" border="0" alt="" />',
            '<img src="comments/smilies/sleepy.gif" border="0" alt="" />',
            '<img src="comments/smilies/smile.gif" border="0" alt="" />',
            '<img src="comments/smilies/tongue.gif" border="0" alt="" />',
            '<img src="comments/smilies/undecided.gif" border="0" alt="" />',
            '<img src="comments/smilies/wink.gif" border="0" alt="" />',

            // For 3rd Party Code
            '<pre class="brush: plain;">$1</pre>', 
            '<pre class="brush: php;">$1</pre>', 
            '<pre class="brush: css;">$1</pre>', 
            '<pre class="brush: jscript;">$1</pre>',
            '<pre class="brush: sql;">$1</pre>',
            '<pre class="brush: perl;">$1</pre>',               
            );  


    // Do simple BBCode's  `
   $str = preg_replace ($simple_search, $simple_replace, $str);   
    return $str;  
}  


function getComments($tutid){

//fetch all comments from database where the tutorial number is the one you are asking for:
    $results = mysql_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());
//find the number of comments:
    $commentNum = mysql_num_rows($results);

        echo' <div class="post-bottom-section">
            <h2> '.$commentNum.' comments so far (<a href="#post" title="post your own">post your own</a>)</h2>
             <div class="primary">
                <ol class="commentlist">';

   $count = mysql_numrows($results);
   $i = 0;
   while ($i < $count){


        $qazi_id = mysql_result($results,$i,"qazi_id");
        $name = mysql_result($results,$i,"name");
        $email = mysql_result($results,$i,"email");
        $description =mysql_result($results,$i,"description");      
        $url = mysql_result($results,$i,"url");
        $date = mysql_result($results,$i,"date");

                        echo' <li class="'.$css.'">
                            <div class="comment-info">
                                <img alt="" src="'.$grav_url.' class="avatar" height="42" width="42" />
                                <cite>
                                    <a href="'.$url.'" title="'.$name.'">'.$name.'</a> Says: <br />
                                    <span class="comment-data"><a href="'.$url.'" title="'.$date.'">'.$date.'</a></span>
                                </cite>
                            </div>
                            <div class="comment-text">


                            <p>'.BBCode($description).'</p>                     
                            </div>                          
                                </li>';

        $i++;

        }
                     echo'</ol></div></div>';
                 }`

If I replace $description =mysql_result($results,$i,"description"); with $description =nl2br(mysql_result($results,$i,"description")); the code displays output as

#include&lt;stdio.h&gt;&lt;br /&gt; main() &lt;br /&gt; { &lt;br /%gt; printf("hi");&lt;br /&gt; }

<br /> tags appears.

Jest replace return $str;
with return nl2br($str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM