This applies to BBCode in general: re-run the replacement code until it stops changing it.
preg_replace_callback
accepts a "count" reference variable, which will be filled with the number of replacements made. So long as this number is not zero, you should re-run the replacement (do..while
is perfect for this)
It doesn't matter that they're crossed over. Let's say we have a BBCode that replaces [div]
with <div>
...
[div]Blah[div]123[/div]Fish[/div]
After one replacement:
<div>Blah[div]123</div>Fish[/div]
After another replacement:
<div>Blah<div>123</div>Fish</div>
So even though they were processed in a crossed-over order, the result is properly nested.