I want to write symple (consisting of one preg_replace call) forum parser and I run into problems with nested tags.
E.g. if someone is quoting someone quoting someone, I cannot achieve correct behaviour.
When having:
[quote=Tom]
[quote=Jerry]
Lorem
[/quote]
Ipsum
[/quote]
Dolor.
I want something like this:
<blockquote>
<p><strong>Tom wrote</strong></p>
<blockquote>
<p><strong>Jerry wrote:</strong></p>
<p>Lorem</p>
</blockquote>
Ipsum
</blockquote>
Dolor.
I have this code:
preg_replace('~\[quote (.+)\](.+)\[/quote\]~is', '<blockquote><p><strong>$1</strong> wrote:</p><p>$2</p></blockquote>', $value);
This version is greedy. If I have two separate [quote] blocks, the regex wraps all the text between the first [quote] and the second [/quote].
If I add the U modifier, it’s too ungreedy – the first [quote] tag is paired with the first (nested and irrelevant) [/quote] tag.
Thanks for any help!
There is the PEAR HTML_BBCodeParser Package and also PHP has a native extension for parsing code like this, check this example: http://www.php.net/manual/en/function.bbcode-create.php