Explode string when not between ()
You need to use preg_split
which splits the input according to the given regex.
$words = preg_split('~,(?![^()]*\))~', $str);
print_r($words);
DEMO
Explanation:
,
matches all the commas, only if it's not- followed by any char but not of
(
or)
, zero or more times - and further followed by a closing bracket.
If you change (?!
to (?=
, it does the opposite of matching all the commas which are present inside the brackets.