note 111928 added to function.json-decode

From: Date: Fri, 12 Apr 2013 21:39:28 +0000
Subject: note 111928 added to function.json-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-194466@lists.php.net to get a copy of this message
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" } */ ?> ---- Server IP: 69.147.83.199 Probable Submitter: 184.70.11.214 ---- Manual Page -- http://php.net/manual/en/function.json-decode.php Edit -- https://master.php.net/note/edit/111928 Del: integrated -- https://master.php.net/note/delete/111928/integrated Del: useless -- https://master.php.net/note/delete/111928/useless Del: bad code -- https://master.php.net/note/delete/111928/bad+code Del: spam -- https://master.php.net/note/delete/111928/spam Del: non-english -- https://master.php.net/note/delete/111928/non-english Del: in docs -- https://master.php.net/note/delete/111928/in+docs Del: other reasons-- https://master.php.net/note/delete/111928 Reject -- https://master.php.net/note/reject/111928 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194466) next »