PHP json_encode trunk string when using international character like é
This simple code show you the problem:
class MyObject
{
var $publicString = "This is a weird character : é and it will trunk this sentence";
}
$myObject = new MyObject();
var_dump(json_encode($myObject));
The var_dump output is :
string(47) "{"publicString":"This is a weird c开发者_如何学JAVAharacter : "}"
Why?
json_encode()
expects UTF-8 data.
I assume your file is ISO-8859-1 encoded. ISO-8859-1 é
is an invalid character in UTF-8.
A workaround would be storing the file as UTF-8, or doing an iconv()
:
$myObject->publicString =
iconv("iso-8859-1", "utf-8//IGNORE", $myObject->publicString);
var_dump(json_encode($myObject));
json_encode()
only works with UTF-8 char set. Here's a link to an example on how to deal with this issue.
精彩评论