Reputation: 117
So, for basic stuff my regex is working fine.
eg
[b]text[/b]
becomes <b>text</b>
but
[b]te[b]x[/b]t[/b]
becomes <b>te[b]x</b>t[/b]
ideally i want it: <b>te<b>x</b>t</b>
If i do a different bbcode, it works:
[b]te[i]x[/i]t[/b]
becomes <b>te<i>x</i>t</b>
The reason this is an issue, is because i have a quote bbcode, and sometimes people will end up quoting someones post, which itself has quoted someone else.
The regex looks like this:
$str = preg_replace("/\[b\](.*?)\[\/b\]/misS", "<b>$1</b>", $str);
Upvotes: 0
Views: 117
Reputation: 6566
You can do this easily with a regex as long as you are willing to be fairly dumb: don't worry about finding matched pairs, just replace anything that looks like an opening or closing tag:
$str = preg_replace("|\[(/?\w+)\]|iS", "<$1>", $str);
If something like that is not good enough, then a regex is not your solution. It can't handle nested elements properly because it is not a real parser.
There is a PHP extension for parsing BBCode. That is probably the way to go if you are interested in robust, correct parsing (caveat: haven't used it myself).
Upvotes: 1