|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51947 json_encode fails to decode non-breaking spaces
Submitted: 2010-05-29 01:24 UTC Modified: 2010-05-31 05:08 UTC
From: ehsan dot akhgari at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.2 OS: Windows, Linux
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:
Bug Type:
From: ehsan dot akhgari at gmail dot com
New email:
PHP Version: OS:


 [2010-05-29 01:24 UTC] ehsan dot akhgari at gmail dot com
I've seen that json_encode cannot encode a non-breaking space character, and returns "null".  I've reproduced this on php5.3.2 on Windows and Linux.

Test script:



Expected result:
string(3) "" ""

Actual result:
string(4) "null"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-29 09:46 UTC] arpad dot borsos at googlemail dot com
I have an outdated 5.2.4 on my server which returns an empty string instead of null in that case.

Anyway, 0xa0 (160) is not a valid utf8 character (it needs to be 0xc2 0xa0) and thus invalid json. I believe PHP is right about erroring out in this case.



$encutf8 = json_encode(chr(0xc2).chr(0xa0));

both work fine.

Can you check the output of json_last_error() please?
 [2010-05-31 05:08 UTC]
-Status: Open +Status: Bogus
 [2010-05-31 05:08 UTC]
This is expected behaviour: as Arpad says, json_encode() expects input to be encoded in UTF-8. json_encode(utf8_encode(chr(160))) behaves as you'd expect: you get "\u00a0" as the result.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 29 21:01:30 2024 UTC