user1392191
user1392191

Reputation: 117

PHP bbcode recursive regex

[div="desc"][1="text1"][2="text2"][3="link3"][/div]

And it continue could continue to a limit of 6. How could create a regex that extract the "desc", the numbers and the texts? I tried with php array, but i wasn't able.

Thank you!

Upvotes: 0

Views: 273

Answers (1)

Ansari
Ansari

Reputation: 8218

There is no recursion here - at least your example doesn't show it. If there does turn out to be recursion in your general case, regexes won't work; you will have to use a parser.

For this case, I would suggest first using preg_match_all with a regex like /[([^]]+)]/ . If you pass an array matches to this function, after running preg_match_all, matches[1] should be an array containing the strings '[div="desc"]', '[1="text1"]', etc. Once you have this array, you can loop through it using foreach and split each string on = and then do whatever you like with the two parts of the string that you parsed.

Upvotes: 2

Related Questions