Headchopperz
Headchopperz

Reputation: 117

Regex for BBCodes, not working if a BB Code is inside itself (PHP)

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

Answers (2)

dan1111
dan1111

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

Anirudha
Anirudha

Reputation: 32797

Use \[(/?b)\] and replace it with <$1>

Upvotes: 1

Related Questions