I split a string by comma, but not within parathesis, using preg_split
. I came up with
preg_split('#,(?![^\(]*[\)])#',$str);
which works perfectly unless there is a comma before a nested parenthesis.
Works for
$str = "first (1,2),second (child (nested), child2), third";
Array
(
[0] => first (1,2)
[1] => second (child (nested), child2)
[2] => third
)
but not for
$str = "first (1,2),second (child, (nested), child2), third";
Array
(
[0] => first (1,2)
[1] => second (child
[2] => (nested), child2)
[3] => third
)