Jump to content

PHP BBC Code help


Attila

Recommended Posts

Not sure what I am doing wrong here.  So some help and guidance would be great.  It dosen't apear to be converting the BBC code to what it should be.  Here is my coding.

 

The value going into the functions is $str

 

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

    $simple_search = array( 
                '/\[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',         
                ); 

    $simple_replace = array( 
                '<strong>$1</strong>', 
                '<em>$1</em>', 
                '<u>$1</u>', 
                '<a href="$1">$2</a>', 
                '<a href="$1">$1</a>', 
                '<div style="text-align: $1;">$2</div>', 
                '<img src="$1" />', 
                '<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>', 
                ); 

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

    // Do <blockquote> BBCode 
    $str = bbcode_quote ($str); 

    return $str; 
} 



function bbcode_quote ($str) { 
    $open = '<blockquote>'; 
    $close = '</blockquote>'; 

    // How often is the open tag? 
    preg_match_all ('/\[quote\]/i', $str, $matches); 
    $opentags = count($matches['0']); 

    // How often is the close tag? 
    preg_match_all ('/\[\/quote\]/i', $str, $matches); 
    $closetags = count($matches['0']); 

    // Check how many tags have been unclosed 
    // And add the unclosing tag at the end of the message 
    $unclosed = $opentags - $closetags; 
    for ($i = 0; $i < $unclosed; $i++) { 
        $str .= '</blockquote>'; 
    } 

    // Do replacement 
    $str = str_replace ('[' . 'quote]', $open, $str); 
    $str = str_replace ('[/' . 'quote]', $close, $str); 

    return $str; 
} 

 

Being new at this I am not sure if I am suppost to call both functions when putting the data into the database.  Or do I just call them when showing the data on the webpage?  If the webpage what one do I show?

Link to comment
https://forums.phpfreaks.com/topic/122960-php-bbc-code-help/
Share on other sites

You should be calling the functions when you output the data. Generally, you should store data in it's original format. Consider what happens when people wish to edit their posts - if you make the replacements before you put the data in the database, you'll have to reverse those replacements before the user could edit their posts.

 

As for which to call, it looks like you should call the bbcode_format() function first, followed by the bbcode_quote() function.

Link to comment
https://forums.phpfreaks.com/topic/122960-php-bbc-code-help/#findComment-635089
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.