3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "['text', 'te\\'\"x,2t', \"tex,t\", \"te\\\"x'#t\", true, True, [false, False, 100, +100], -100, + 10, - 20]"; $parts = preg_split('/\s*(\\\\["\']|"(?:\\\\"|[^"])*"|\'(?:\\\\\'|[^\'])*\')\s*|\s*([,\[\]])\s*/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($parts as &$part) { if (preg_match("/^'(.*)'/", $part, $matches)) { $part = '"' . str_replace(array('"', "\\'"), array('\\"', "'"), $matches[1]) . '"'; continue; } $lpart = strtolower($part); if ($lpart == 'true' || $lpart == 'false') { $part = $lpart; continue; } if (preg_match('/^\+\s*(\d+)$/', $part, $matches)) { $part = $matches[1]; continue; } if (preg_match('/^-\s*(\d+)$/', $part, $matches)) { $part = '-' . $matches[1]; continue; } } $json = implode('', $parts); var_dump(json_decode($json));

preferences:
117.69 ms | 410 KiB | 5 Q