php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #57461 Error with escaped backslash
Submitted: 2006-12-28 07:36 UTC Modified: 2013-02-18 00:35 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: alex at passant dot org Assigned:
Status: No Feedback Package: json (PECL)
PHP Version: 5.2.0 RC4 OS: Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: alex at passant dot org
New email:
PHP Version: OS:

 

 [2006-12-28 07:36 UTC] alex at passant dot org
Description:
------------
When a string contains \\, json_decode returns no result (while \/ works fine).


Reproduce code:
---------------
{
  "head": {
    "vars": ["site", "name"]
  },
  "results": {
    "distinct": true,
    "bindings": [
      {
        "site": { "type": "uri", "value": "http://b4mad.net/datenbrei/" },
        "name": { "type": "literal", "value": "B:\\datenbrei" }
      }
    ]
  }
}


Expected result:
----------------
stdClass Object
(
    [head] => stdClass Object
        (
            [vars] => Array
                (
                    [0] => site
                    [1] => name
                )

        )

    [results] => stdClass Object
        (
            [distinct] => 1
            [bindings] => Array
                (
                    [0] => stdClass Object
                        (
                            [site] => stdClass Object
                                (
                                    [type] => uri
                                    [value] => http://b4mad.net/datenbrei/
                                )

                            [name] => stdClass Object
                                (
                                    [type] => literal
                                    [value] => B:\datenbrei
                                )

                        )

                )

        )

)

Actual result:
--------------
None

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-18 21:46 UTC] scottmac@php.net
I can't reproduce this with 5.2.6.

<?php
$a = array('name' => 'B:\datenbrei');
var_dump(json_encode($a));
var_dump(json_decode(json_encode($a)));
?>

Produces
string(24) "{"name":"B:\\datenbrei"}"
object(stdClass)#1 (1) {
  ["name"]=>
  string(12) "B:\datenbrei"
}
 [2012-08-02 11:44 UTC] lonny at lonnylot dot com
I can reproduce this on 5.4.5-dev

scottmac@php.net - your test is wrong. You need to have two backslashes, not one
 [2012-08-30 15:04 UTC] lonnyk at gmail dot com
The issue stems from the fact that when you set a string in a PHP script and it 
contains \\. The \\ is _almost_ *always* interpreted by PHP and stored internally 
as a single \. Because a backslash always needs to be escaped in a json object 
decoding the string always fails.

Currently the only ways to set a string in your script is to either triple escape 
your backslash (\\\) or use a NOWDOC.

The only other option I can come up with is to allow json_decode to take invalid 
json strings (and valid json strings). Here is what that patch would look like: 
https://github.com/lonnylot/php-src/compare/issue-57461

I'm not sure if it would be better to accept invalid json strings or if it would 
be better to just make a note of this in the PHP documentation.
 [2013-02-18 00:35 UTC] pecl-dev at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Nov 14 18:01:35 2019 UTC