note 99691 added to function.json-decode
| From: | alex dot bergin at gmail dot com | Date: | Tue, 31 Aug 2010 06:39:02 +0000 |
| Subject: | note 99691 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172189@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 202.72.133.57
----
Manual Page -- http://www.php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/99691
Del: integrated -- https://master.php.net/note/delete/99691/integrated
Del: useless -- https://master.php.net/note/delete/99691/useless
Del: bad code -- https://master.php.net/note/delete/99691/bad+code
Del: spam -- https://master.php.net/note/delete/99691/spam
Del: non-english -- https://master.php.net/note/delete/99691/non-english
Del: in docs -- https://master.php.net/note/delete/99691/in+docs
Del: other reasons-- https://master.php.net/note/delete/99691
Reject -- https://master.php.net/note/reject/99691
Search -- https://master.php.net/manage/user-notes.php