|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49588 json_encode returns null for certain strings
Submitted: 2009-09-18 12:36 UTC Modified: 2009-09-18 13:29 UTC
From: fernando at consultorpc dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.0 OS: Linux/Mac OSX Leopard
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: fernando at consultorpc dot com
New email:
PHP Version: OS:


 [2009-09-18 12:36 UTC] fernando at consultorpc dot com
At my specific case, if a string contains an Euro symbol ( ? ), json_encode will return null for that string.

It might also happens with other special characters.

Reproduce code:

$array = array(
        'name' => htmlentities( 'My euro symbol ?.' )

var_dump( $array );
var_dump( json_encode( $array ) );

Expected result:
array(1) {
  string(29) "My euro symbol &euro;."
string(13) "{"name":"My euro symbol &euro;."}"

Actual result:
array(1) {
  string(29) "My euro symbol &acirc;?&not;."
string(13) "{"name":null}"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-18 12:54 UTC]
PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in Command line code on line 1
 [2009-09-18 13:29 UTC]
It only appears of display_errors is on though with 5.3.1+ you can also detect it by looking at json_last_error()
 [2010-04-01 15:13 UTC] arievanziel at gmail dot com
json_encode returns null for a string with é or (for example) ä in it.

Reproduce code:

$test = "é";
echo $test;
echo json_encode($test);

A possible workaround could be to use (especially if you will use the 
json_encode string as html output):

$test = "é";
$test = htmlentities($test, UTF-8);
echo $test;
echo json_encode($test);
 [2010-04-07 13:21 UTC] pcdinh at gmail dot com
Hi arievanziel,

In your case, the character 'é' may be encoded in 'latin1'. If you save the code in a UTF-8 encoded file you will find that the result will be 


as expected
 [2010-12-27 23:19 UTC] gtrujillos at gmail dot com
A use full solution:
 [2010-12-27 23:20 UTC] gtrujillos at gmail dot com
A useful solution:
 [2012-07-02 16:58 UTC] gudjonj at gmail dot com
To return null if encoding is incorrect makes no sense.
 [2013-10-28 17:09 UTC] herrero dot olivari at gmail dot com
I had the same problem and solved it using utf8_encode...

the weird thing is I'm fetching information from the database that is in UTF8
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 01:01:30 2024 UTC