|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-10-29 15:14 UTC] nadavvin at gmail dot com
Description:
------------
json_encode failed to parse if there is '\' or '\\' but if there more it represent all as one '\' even if there is five.
* Firefox can parse all the strings
Test script:
---------------
$a = '{"A":"C\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";
$a = '{"A":"C\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";
$a = '{"A":"C\\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";
$a = '{"A":"C\\\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";
$a = '{"A":"C\\\\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";
Expected result:
----------------
all parse, and the result of '\\\\' should be '\\'
Actual result:
--------------
syntax error in '\', '\\'
'\\\', '\\\\', '\\\\\' all represent as one '\'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 20 06:00:02 2025 UTC |
No, it's correct. You have a JavaScript String inside a PHP string, so PHP itself is parsing slashes. E.g. the PHP code '{"A":"C\\\\D"}' results in the string {"A":"C\\D"}. The embedded JavaScript string is C\D. var_dump the $a strings before decoding, or read the JSON data from a file instead of embedding it in code, and you'll see what I mean.