This is difficult for regular expression, but there is an answer to your question (apologies in advance).
A string is an almost valid array literal, but for ,, s. You can match these pairs and then convert to ,'' with
/,(?=,)/
You can then eval to include this string in the output array you are looking for.
For instance:
// input $str1 = '[[["Hello, \\"how\\" are you?","Good!",,,123]],,"ok"]'; // replace , followed by , with ,'' with a regex $pattern = '/,(?=,)/'; $replace = ",''"; $str2 = preg_replace($pattern, $replace, $str1); // eval updated string $arr = eval("return $str2;"); var_dump($arr);
I get this:
array(3) { [0]=> array(1) { [0]=> array(5) { [0]=> string(21) "Hello, "how" are you?" [1]=> string(5) "Good!" [2]=> string(0) "" [3]=> string(0) "" [4]=> int(123) } } [1]=> string(0) "" [2]=> string(2) "ok" }
Edit
Noting the inherent danger of eval , the best option is to use json_decode with the code above, for example:
// input $str1 = '[[["Hello, \\"how\\" are you?","Good!",,,123]],,"ok"]'; // replace , followed by , with ,'' with a regex $pattern = '/,(?=,)/'; $replace = ',""'; $str2 = preg_replace($pattern, $replace, $str1); // eval updated string $arr = json_decode($str2); var_dump($arr);