note 111928 deleted from function.json-decode by salathe

From: Date: Mon, 21 Sep 2020 15:01:52 +0000
Subject: note 111928 deleted from function.json-decode by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215440@lists.php.net to get a copy of this message
Note Submitter: phpdoc at badassawesome dot com ---- I added a 3rd regex to the json_decode_nice function by "colin.mollenhour.com" to handle a trailing comma in json definition. <?php // http://www.php.net/manual/en/function.json-decode.php#95782 function json_decode_nice($json, $assoc = FALSE){ $json = str_replace(array("\n","\r"),"",$json); $json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','$1"$3":',$json); $json = preg_replace('/(,)\s*}$/','}',$json); return json_decode($json,$assoc); } ?> Example: <?php $dat_json = <<<EOF { "foo" : "bam", "bar" : "baz", } EOF; $dat_array = json_decode_nice( $dat_json ); var_dump ( $dat_json, $dat_array ); /* RESULTS: string(35) "{ "foo" : "bam", "bar" : "baz", }" array(2) { ["foo"]=> string(3) "bam" ["bar"]=> string(3) "baz" } */ ?>

« previous php.notes (#215440) next »