Regular Expression to split string into nested array
I have this 开发者_如何学运维string: 1[2[5,6]],4,7[8,10] It represents nested IDs to be used to build an array of navigation items.
I need to parse this string and create a multidimensional array of its values. This will probably take several consecutive expressions and loops to build.
The end product would be an array like this:
1 =>
2 =>
5
6
4
7 =>
8
10
I think all I need is an expression that will split the string on commas not inside of a set of brackets. I would then loop over the substrings with the same pattern.
I realize this doesn't directly answer your question, but there's really no point in making your own parser. Just store the data in a JSON string instead.
http://php.net/json-encode
http://php.net/json-decode
精彩评论