Reputation: 2851
I currently have the following regex:
\[quote="([^"]+)"\]([^[]+)\[\/quote\]
Which matches [quote="author"]text[/quote]
in a string and captures author
and text
to convert it to a formatted div quote:
$p_text = preg_replace('/\[quote="([^"]+)"\]([^[]+)\[\/quote\]/', '<div class="quote"><strong class="quote-author">$1 wrote:</strong><br>$2</div>', $p_text);
However I want to be able to nest it once, like this:
[quote="b"]
[quote="a"]
Original text
[/quote]
Reply to quote a
[/quote]
Reply to quote b
Here's how I want it to look:
Here's how it currently looks with the code above:
How can I nest the quotes?
Upvotes: 2
Views: 235
Reputation: 18490
You can resolve it from inside out with your current regex by using a loop.
$pattern = '/\[quote="([^"]+)"\]([^[]+)\[\/quote\]/';
$replace = '<div class="quote"><strong class="quote-author">' .
'$1 wrote:</strong><br>$2</div>';
do {
$p_text = preg_replace($pattern, $replace, $p_text, -1, $replace_count);
} while($replace_count > 0);
See demo at eval.in. If you need a recursive regex for whatever reason, see this demo at regex101.
Upvotes: 1