For your example, if you don't want to use regex and want to stick with the explode()
function you are already using, you could simply replace all instances of ', '
with ','
, then break the string in parts by ,
(followed by a space) instead of just the comma.
This makes it so things inside the brackets don't have the explode delimiter, thus making them not break apart into the array.
This has an additional problem, if you had a string like '==', 'test-taco'
, this solution would not work. This problem, along with many other problems probably, can be solved by removing the single quotes from the separate strings, as ==, test-taco
would still work.
This solution should work if your strings inside brackets are valid PHP arrays/JSON string
$str = "'==', ['abc', 'xyz'], 1";
$str = str_replace("', '", "','", $str);
$str = explode(", ", $str);
Though I recommend regex as it may solve some underlying issues that I don't see.
Output is:
Array
(
[0] => '=='
[1] => ['abc','xyz']
[2] => 1
)