Skip to content

Instantly share code, notes, and snippets.

@donamkhanh
Created April 7, 2010 09:54
Show Gist options
  • Save donamkhanh/358712 to your computer and use it in GitHub Desktop.
Save donamkhanh/358712 to your computer and use it in GitHub Desktop.
json_encode returns null for certain strings. See: http://bugs.php.net/49588
<?php
$a = "Hello, I'm an other charset éc éc";
$b = htmlentities($a, ENT_QUOTES, 'UTF-8');
$c = htmlentities($a);
echo "\n", json_encode($a); //ouput will be "Hello, I'm an other charset "
echo "\n", json_encode($b); //ouput will be "" (NULL)
echo "\n", json_encode($c); //ouput will be "Hello, I'm an other charset &eacute;c &eacute;c"
@pcdinh
Copy link

pcdinh commented Apr 7, 2010

If the file is encoded correctly

string(45) ""Hello, I'm an other charset \u00e9c \u00e9c""
string(54) ""Hello, I'm an other charset éc éc""
string(61) ""Hello, I'm an other charset éc éc""

@donamkhanh
Copy link
Author

Thanks for your comment. I thought I still can check the character encoding based on input (signed - but still under ACSII encoding) but forgot about the file encoding (when I test). And the "character set" of the DB too :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment