nl2br不使用BBCode

时间:2013-11-16 21:25:26

标签: php mysql bbcode nl2br

我的表中有数据如下:

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

但是当我从数据库中检索数据时,它会显示:

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

如您所见,没有换行符。这是我的整个代码:

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>';
                 }`

如果我将$description =mysql_result($results,$i,"description");替换为$description =nl2br(mysql_result($results,$i,"description"));,则代码会将输出显示为

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

&lt; br /&gt;标签出现。

1 个答案:

答案 0 :(得分:0)

Jest替换return $ str;
同 return nl2br($ str);