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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.