Reputation: 452
This is a kind of BB codes. Any idea how to match all elements like [LI]text[/LI] and [UL]text[/UL]?
preg_match_all("/(\[UL].*\[\/UL])|(\[LI].*\[\/LI])/", '[UL][LI]sadas[/LI][/UL]', $match);
Want to receive something like:
0 => "[UL][LI]sadas[/LI][/UL]"
1 => "[UL][LI]sadas[/LI][/UL]"
2 => "[LI]sadas[/LI]" <--- This is not captured now.
Basically it is about: How to get this [LI]text[/LI] part and not loose [UL]text[/UL] part?
Upvotes: 0
Views: 107
Reputation: 89547
To do that you need 2 things:
~(?=(\[(\w+)]([^[]*(?:(?1)[^[]*)*?)\[/\2]))~
(?=...)
is the lookahead assertion. (the current position is followed by ...)
(\[(\w+)]([^[]*(?:(?1)[^[]*)*?)\[/\2])
is the capture group 1.
(?1)
refers to the subpattern inside the capture group 1.
\2
refers to the match of the capture group 2 (the tag name).
Upvotes: 2