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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
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
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

Pull Requests

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: Sun Dec 22 01:01:30 2024 UTC