I am trying to make myself a BBCODE parser in PHP.
Now I have the following Regex:
\[quote\](.*?)\[\/quote\]
This should replace with:
<div class='quote'><div class='quotetext'>$1</div></div>
This works all perfect until i have a "multidimentional" post Example:
[quote] [quote] [quote] text [/quote] [/quote] [/quote]
This should have the following outcome:
<div class='quote'><div class='quotetext'>
<div class='quote'><div class='quotetext'>
<div class='quote'><div class='quotetext'>
text
</div></div>
</div></div>
</div></div>
Right now it gets the following outcome:
<div class='quote'><div class='quotetext'> [quote] [quote] text </div></div> [/quote] [/quote]
Php:
preg_replace("/\[quote\](.*?)\[\/quote\]/", "<div class='quote'><div class='quotetext'>$1</div></div>", $text);
I hope someone could help me with this issue. Thanks
preg_replace()probably doesn't do what you think it does, it takes the first[quote]and the first[/quote], not the outer ones. In this case using regular expressions will probably not be the correct solution. Yes, they do have a place in finding things when making this parser, but without building a semi-real DOM, like HTML has, I don't think this will ever work.[quote]by<div class='quote'><div class='quotetext'>. And 2. Replace[/quote]by</div></div>. As long as the BBCODE is valid this should work out fine.