Better way to handle bbcode?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Better way to handle bbcode?

Post by Cirdan »

So I'm currently making a clan web site, and am implementing my own bbcode. Currently, I have an array of regex expressions that replace the bbcode with the appropriate html.

For example

Code: Select all

'/\[color=(.*?)\](.*?)\[\/color\]/'   => "<span style='color:\\1'>\\2</span>",
This works, however, it does not support nested tags. If I have a quote tag inside a quote tag it breaks one of the pairs of quotes (I'd also like to prevent some things like quoting youtube videos). Is there a better way to parse the code? I can't just replace each bbcode with the equivalent because we always need a closing tag. What I would have to do is basically loop through the text and replace the bbcode then create a list or queue to keep track of the parsed bbcode. Then track which ones didn't have a closing tag, and either error out or place the closing tag at the end of the post. I'm just not sure how I would keep track of the pairs. Has anyone implemented a similar system have any advice?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Better way to handle bbcode?

Post by jraede »

There's a bunch of BBCode parsers already out there; no need to reinvent the wheel. Just do a search on google.
Post Reply