3v4l.org

run code in 300+ PHP versions simultaneously
<?php // stackoverflow.com/questions/20348380 function fixJSON($json) { $regex = <<<'REGEX' ~ "[^"\\]*(?:\\.|[^"\\]*)*" (*SKIP)(*F) | '([^'\\]*(?:\\.|[^'\\]*)*)' ~x REGEX; return preg_replace_callback($regex, function($matches) { return '"' . preg_replace('~\\\\.(*SKIP)(*F)|"~', '\\"', $matches[1]) . '"'; }, $json); } $json = "{'id': '3', 'answer': '1'},{'id': '4', 'answer': 'здесь ответ на вопрос, 12345 abc'},{'id': '4', 'answer': '123'}"; $obj = json_decode(fixJSON("[$json]")); foreach ($obj as $key=>$val) { print_r($val->answer); echo "\n"; }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
1 здесь ответ на вопрос, 12345 abc 123
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1 здесь ответ на вопрос, 12345 abc 123

preferences:
152.84 ms | 402 KiB | 181 Q