note 99691 deleted from function.json-decode by danbrown
| From: | danbrown@php.net | Date: | Tue, 31 Aug 2010 14:57:57 +0000 |
| Subject: | note 99691 deleted from function.json-decode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172202@lists.php.net to get a copy of this message | ||
Note Submitter: alex dot bergin at gmail dot com
----
The comment by <a href="http://www.php.net/manual/en/function.json-decode.php#91216">www
at walidator dot info</a> does not suit content which includes inverted comma's. As a
result you'll get "syntax error, unexpected ':', expecting ')'".
Try the following function instead:
<?php
if ( !function_exists('json_decode') ){
function json_decode($json) {
// Author: walidator.info 2009, tweaked by akb.id.au 2010
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++) {
if (!$comment) {
if ($json[$i] == '[')
$out .= ' array(';
else if ($json[$i] == ']')
$out .= ')';
else if ($json[$i] == '{')
$out .= ' array(';
else if ($json[$i] == '}')
$out .= ')';
else if ($json[$i] == ':')
$out .= '=>';
else
$out .= $json[$i];
}
else {
$out .= $json[$i];
}
if ($json[$i] == '"' && $json[$i-1] != "\\")
$comment = !$comment;
}
eval($out . ';');
return $x;
}
}
?>