I’m beginner with PHP and I try to use preg_math_all to split a string.
My string look like to :
[0, 5, 2, 1, true, COMMENT, 1][0, 27, 4, 1, true, COMMENT 2, 2]
The string may contain several part with […].
So, I try to use preg_match_all, like that:
preg_match_all('/\[\s*?(\d+), \s*?(\d+), \s*?(\d+), \s*?(\d+), \s*?(true|false), (\w+), \s*?(\d+)\]/i', $string, $matches, PREG_SET_ORDER);
But the result is no agree with my hope, could you help me to solve that.
Thanks
I would use something like this:
Instead of trying to match every component individually, just match everything that’s in the square brackets, and then do some additional parsing to get everything in its own array element.
Output:
Demo
Alternatively, you could just use
explodeand do some more processing, like so:This will yield the same output as above.