   [web] BBCode in PHP
  Does anyone know of a good BBCode parser for PHP? I have been poking around the internet for a while now and keep finding something wrong with the ones I have tried out. It looks like I'm going to have to hack away at one of them to try and fix the problems, but I'd rather have something that just works. I mean, tons of websites use BBCode, where did they get their implementations from?   
  2 points for you:- BBCode is lame. Use richtext! You would be surprised how insanely easy it is to get this thing up and running:FCKEditorI've tried a few of them, and liked this one the best. It's fast, light, and doesn't come with a lot of glop. It also puts out correct XHTML (big plus!)- If you are bent on BBCode, try this one. I have a hacked version with a few additional features added too, if you are interested.;  There are two Pear libraries who can do that: HTML_BBCodeParser and Text_Wiki_BBCode.   ;  Of course you could also lookup the sourcecode of software that uses BBCode. The one used by phpBB for example should also be capable of bbcode with arguments (example: we once had a tag that goes like [img width=512]img.jpg[/img] on ompf.org).   ;Quote:Original post by leiavoiaBBCode is lame. Use richtext!That is cool as hell. But I think it is extreme overkill for what I am doing (I just want to allow a bit of formatting to users, hyperlinks, etc).Quote:If you are bent on BBCode, try this one. I have a hacked version with a few additional features added too, if you are interested.Thanks I'll look into it.Quote:Original post by SanderThere are two Pear libraries who can do that: HTML_BBCodeParser and Text_Wiki_BBCode.HTML_BBCodeParser is the one I have most recently played with, but it apparently has some bugs that have been open for QUITE a while. I'd rather not have to mess with it too much, but also I don't want to leave the opportunity for some random internet idiot to inject &#106avascript into my site. The Wiki one is very early alpha.Quote:Original post by phresnelOf course you could also lookup the sourcecode of software that uses BBCode. The one used by phpBB for example should also be capable of bbcode with arguments (example: we once had a tag that goes like [img width=512]img.jpg[/img] on ompf.org).I might have to, but I thought there might be a good one already packaged up for me to download and forget about.Thanks all of you!   ;Quote:Original post by krezQuote:Original post by leiavoiaBBCode is lame. Use richtext!That is cool as hell. But I think it is extreme overkill for what I am doing (I just want to allow a bit of formatting to users, hyperlinks, etc).Did you try the basic layout?   ;Quote:Original post by krezQuote:Original post by phresnelOf course you could also lookup the sourcecode of software that uses BBCode. The one used by phpBB for example should also be capable of bbcode with arguments (example: we once had a tag that goes like [img width=512]img.jpg[/img] on ompf.org).I might have to, but I thought there might be a good one already packaged up for me to download and forget about.Of course there's a chance that phpBB themselves use a third party library. If you can invest some 30 minutes or so, you should be able to find the answer. Last time I checked, the sourcecode was okay to navigate :)   ;Quote:Original post by krezQuote:Original post by leiavoiaBBCode is lame. Use richtext!That is cool as hell. But I think it is extreme overkill for what I am doing (I just want to allow a bit of formatting to users, hyperlinks, etc).No, that's exactly what it's for. FCKeditor lets you configure what options you want available. You can make it really over-the-top or very simple. If all you want is B, I, U, and LINK, you can do that.See here for exampleI would urge you to take a second look. It's too simple to pass up!   ;  Here is some code I found around the net a while ago, fixed up and incorporated into a project. I haven't used it in a while because as leiavoia said I've switched to rich text editor (tho I normally use tinymce because I'm most familiar with it and like the plugin capability.) But it does work and is setup for most of the basic bbcode. If you are familiar with regular expressions then you can easily expand it to any tag you can think of.//bbcode and smiliesfunction bbcode ($str) {    $str = htmlentities($str);    $token = array( "'\[b\](.*?)\[/b\]'is",'/\[i\](.*?)\[\/i\]/is',   '/\[u\](.*?)\[\/u\]/is',  '/\[url\=http:\/\/(.*?)](.*?)\[\/url\]/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',    );    $tokenized = array( '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>','<a href=http://$1 target="blank">$2</a>', '<a href=http://$1 target="blank">$2</a>', '<a href="$1" target="blank">$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 ($token, $tokenized, $str);    // Do <blockquote> BBCode    $str = embedded ($str);    return $str;}function embedded ($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;} ;  But WYSIWYG's produces html-code, which could be abused if you're not careful. You'd have to implement a server-side verification of the generated html, which would be a lot tougher than verifying the simpler BBCode.   