|  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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
30 + 3 = ?
Subscribe to this entry?

 [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-2023 The PHP Group
All rights reserved.
Last updated: Thu Sep 21 18:01:24 2023 UTC