php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77623 jsno_encode handle badly backslashes
Submitted: 2019-02-15 10:07 UTC Modified: 2019-02-15 13:17 UTC
From: marco dot biondi at docomodigital dot com Assigned: cmb (profile)
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
 [2019-02-15 10:07 UTC] marco dot biondi at docomodigital dot com
Description:
------------
---
From manual page: https://php.net/function.json-encode
---

Just look at the sample code

Test script:
---------------
<?php
$a = "Srting with a slash: \ Just a slash is that I want!";
$b = "Srting with a slash: \\ Just a slash is that I want!";

var_dump($a);
var_dump(json_encode($a));

var_dump($b);
var_dump(json_encode($b));


Expected result:
----------------
string(51) "Srting with a slash: \ Just a slash is that I want!"
string(53) ""Srting with a slash: \ Just a slash is that I want!""
string(51) "Srting with a slash: \ Just a slash is that I want!"
string(53) ""Srting with a slash: \ Just a slash is that I want!""


Actual result:
--------------
string(51) "Srting with a slash: \ Just a slash is that I want!"
string(54) ""Srting with a slash: \\ Just a slash is that I want!""
string(51) "Srting with a slash: \ Just a slash is that I want!"
string(54) ""Srting with a slash: \\ Just a slash is that I want!""


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-15 12:45 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-02-15 12:45 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

A single backslash in a JSON string literal would be an error.
 [2019-02-15 13:05 UTC] marco dot biondi at docomodigital dot com
Yes you are true, I tried to oversimplify the sample and the result is confusing. I discovered this problem trying to forward a complex object with a proper unicode literal \u00BF inside a string field. Even in this case the json_encode convert it in \\u00BF breaking the meaning of the initial string
 [2019-02-15 13:17 UTC] cmb@php.net
A proper Unicode escape in PHP would be \u{00BF}, and this works
as expected: <https://3v4l.org/OCqmT>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC