Jump to content


Photo

bbcode type parsing


  • Please log in to reply
1 reply to this topic

#1 RShadow

RShadow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 05:07 PM

I'm attempting to build a parser similar to bbcode style, and I'm not exactly sure how to approach this... all the methods I can think up in my head would require basicly a character by character analayis that would be .. well pretty complex.. I'm sure there is a better way in PHP. Doing something like smilies is realativly simple.. pretty much a simple search and replace.. however I'm not exactly sure how I would go about searching for a match pair such as [ some tag ] [ /some end tag ]

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 July 2006 - 05:50 PM

Heres a simple BBCode parser:
<?php

function bbcode($txt)
{
    // bbcodes
    $bbcodes = array( "|\[b\](.+)\[/b\]|is",
                      "|\[u\](.+)\[/u\]|is",
                      "|\[i\](.+)\[/i\]|is"
                    );

    // html
    $replace = array( "<strong>$1</strong>",
                      "<u>$1</u>",
                      "<em>$1</em>"
                    );

    $txt = preg_replace($bbcodes, $replace, $txt);

    return nl2br($txt);

}

$str = "[b]hey[/b] a [u][i]BBCode parser[/i][/u]! ";

$str = bbcode($str);

echo $str;

?>
converts any text surrounded in b, i and u tags into bold, italics and underline text.

For a emoticon parser, heres a tutorial




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users