Hello fellow developers,
I tried searching for the answer to this, but I couldn't find anything too helpful in this situation. It's possible that I'm not searching the correct terms.
I'm having trouble with this regex. Consider this string:
$str = "(1, 2, 'test (foo) bar'), (3, 4, '(hello,world)')";
I want to end up with a multidimensional array, like this:
$arr = array(
array(1, 2, 'test (foo) bar'),
array(3, 4, '(hello,world)')
);
I figure I could run a regex to split it up separate strings like "(1, 2, 'test (foo) bar')" and "(3, 4, '(hello,world)')", and then run a regex on each of those to split by comma, but as you can see my problem is the data has parentheses and commas in various strings, and I'd like to ignore those.
So far I have this, which does the first part like I wanted, except if there are parentheses in the data, then it breaks.
preg_match_all('/\((.*?)\),?/', $str, $matches);
It gives me this:
Array
(
[0] => Array
(
[0] => (1, 2, 'test (foo)
[1] => (3, 4, '(hello,world)
)
[1] => Array
(
[0] => 1, 2, 'test (foo
[1] => 3, 4, '(hello,world
)
)
It truncates the data, naturally. What can I do to ignore the parentheses that are in quotes? If I can ignore them, then on the next step when I split each of these matches, I'll be able to ignore commas.
Thanks!