An easier way to do this?

Discussion in 'PHP' started by bigrollerdave, Oct 26, 2006.

  1. #1
    I bought a site a while ago and the bbcode looked like this

    
    $preg = array(
    // Font and text manipulation ( [color] [size] [font] [align] )
    '/\[color=(.*?)(?::\w+)?\](.*?)\[\/color(?::\w+)?\]/si'     => "<font color=\"\\1\">\\2</font>",
    '/\[color=(.*?)(?::\w+)?\](.*)$/si'   => "<font color=\"\\1\">\\2</font>",
    '/\[size=(.*?)(?::\w+)?\](.*?)\[\/size(?::\w+)?\]/si'     => "<font size=\"\\1\">\\2</font>",
    '/\[size=(.*?)(?::\w+)?\](.*)$/si'     => "<font size=\"\\1\">\\2</font>",
    '/\[font=(.*?)(?::\w+)?\](.*?)\[\/font(?::\w+)?\]/si'     => "<font face=\"\\1\">\\2</font>",
    '/\[font=(.*?)(?::\w+)?\](.*)$/si'     => "<font face=\"\\1\">\\2</font>",
    '/\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'                 => "<b>\\1</b>",
    '/\[b\](.*)$/si'     => "<b>\\1</b>",
    '/\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si'                 => "<i>\\1</i>",
    '/\[i\](.*)$/si'     => "<i>\\1</i>",
    '/\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si'                 => "<u>\\1</u>",
    '/\[u\](.*)$/si'     => "<u>\\1</u>",
    '/\[align=(.*?)(?::\w+)?\](.*?)\[\/align(?::\w+)?\]/si'   => "<div style=\"text-align:\\1\">\\2</div>",
    '/\[align=(.*?)(?::\w+)?\](.*)$/si'   => "<div style=\"text-align:\\1\">\\2</div>",
    '/\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si'       => "<center>\\1</center>",
    '/\[center\](.*)$/si'   => "<center>\\1</center>",
    '/\[user=(.*?)(?::\w+)?\](.*?)\[\/user\]/si' => '<a href="/profile.php?username=\\1">\\2</a>',
    '/\[user=(.*?)(?::\w+)?\]/si' => '<a href="/profile.php?username=\\1">\\1</a>',
    // [url]
    '/\[url(?::\w+)?\]www\.(.*?)\[\/url(?::\w+)?\]/si'        => "<a href=\"http://www.\\1\">\\1</a>",
    '/\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'             => "<a href=\"\\1\">\\1</a>",
    '/\[url=(.*?)(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'       => "<a href=\"\\1\">\\2</a>",
    '/\[url\](.*?)/si'       => "<a href=\"\\1\">\\1</a>",
    '/\[url=(.*?)(?::\w+)?\](.*?)/si'       => "<a href=\"\\1\">\\2</a>",
    // [img]
    '/\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si'             => "<img src=\"\\1\" width=\"100\" height=\"100\" border=\"0\" />",
    // [quote]
    '/\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si'         => "<div class=\"quote\"><i>Originally posted by </i><b>\\1</b>:<div class=\"quotebody\">\\2</div></div>",
    '/\[quote=(?:&quot;|"|\')?(.*?)["\']?(?:&quot;|"|\')?\](.*?)\[\/quote(?::\w+)?\]/si'   => "<div class=\"quote\"><i>Originally posted by</i>&nbsp;&nbsp;<b>\\1</b>:<div class=\"quotebody\">\\2</div></div>",
    //[marquee]
    '/\[move(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'                 => "<marquee>\\1</marqueeb>",
    '/\[move\](.*)$/si'     => "<marquee>\\1</marquee>",
    // the following lines clean up our output a bit
    '/<ol(.*?)>(?:.*?)<li(.*?)>/si'         => "<ol\\1><li\\2>",
    '/<ul(.*?)>(?:.*?)<li(.*?)>/si'         => "<ul\\1><li\\2>",
    );
    
    PHP:
    And do display the text you would use this

    $text = preg_replace(array_keys($preg), array_values($preg), $text );
    
    echo $text; 
    
    PHP:
    I'm not too familiar with bbcode on the php side I was wondering if anyone knew an easier way to do this? Thanks


    +Sorry for the poor code, like I said I didn't write it.
     
    bigrollerdave, Oct 26, 2006 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I wrote series of articles looking at processing bbcode a while back for the iceteks community. The final article should be of some use to you - BBcode editor: PHP - The sensible approach

    As the name of this final article suggests the first two articles aren't of much use and the code looked a lot like the regex patterns you posted. Brings a chill to the spine.

    In a nutshell there are two PEAR packages for handling BBcode (comparison of the two PEAR BBcode handling packages) but I would recommend HTML_BBCodeParser.
     
    streety, Oct 26, 2006 IP