php.net |  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:
MUST BE VALID
Solve the problem:
14 - 11 = ?
Subscribe to this entry?

 
 [2010-05-29 01:24 UTC] ehsan dot akhgari at gmail dot com
Description:
------------
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:
---------------
<?php

var_dump(json_encode(chr(160)));

?>

Expected result:
----------------
string(3) "" ""

Actual result:
--------------
string(4) "null"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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.

var_dump(json_encode(utf8_encode(chr(0xa0))));

and

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

both work fine.

Can you check the output of json_last_error() please?
 [2010-05-31 05:08 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-05-31 05:08 UTC] aharvey@php.net
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: Tue Apr 16 19:01:31 2024 UTC