php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42708 json_decode: escape characters
Submitted: 2007-09-19 10:19 UTC Modified: 2008-07-22 17:33 UTC
Votes:13
Avg. Score:4.8 ± 0.4
Reproduced:13 of 13 (100.0%)
Same Version:7 (53.8%)
Same OS:8 (61.5%)
From: tjerk dot meesters at muvee dot com Assigned:
Status: Wont fix Package: JSON related
PHP Version: 5.2.4 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tjerk dot meesters at muvee dot com
New email:
PHP Version: OS:

 

 [2007-09-19 10:19 UTC] tjerk dot meesters at muvee dot com
Description:
------------
When a single quote is being escaped, the json_decode() gives some unexpected results.

1. According to the JSON specs, the single quote is not in the list of characters that can follow the escape character (such as n, t, b, r, etc.); however, JavaScript does recognize the single quote so it would be very nice to have as a feature.

2. When wrapping a string with an escaped single quote inside an array context, the json_decode() gives up and returns NULL.

Reproduce code:
---------------
<?php

$s = '"Sir, you\\\'re an idiot!"';
var_dump(json_decode($s));
var_dump(json_decode("[$s]"));

?>


Expected result:
----------------
string(21) "Sir, you're an idiot!"
array(1) {
  [0]=>
  string(21) "Sir, you're an idiot!"
}

Actual result:
--------------
string(22) "Sir, you\'re an idiot!"
NULL


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-23 00:45 UTC] felipe@php.net
php5.3-200711221930:

string(22) "Sir, you\'re an idiot!"
NULL
--

php6.0-200711202130:

string(22) "Sir, you\'re an idiot!"
string(26) "["Sir, you\'re an idiot!"]"
 [2008-07-22 17:33 UTC] jani@php.net
1. "Fixing" this would violate the JSON spec. HEAD (aka PHP 6) also behaves the same way now as other branches.

2. NULL is returned whenever you pass invalid data to json_decode().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 07:01:27 2024 UTC