且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么PHP json_encode函数将UTF-8字符串转换为十六进制实体?

更新时间:2023-11-27 17:32:10

由于PHP / 5.4.0有一个选项称为JSON_UNESCAPED_UNICODE。查看:



http://se2.php。 net / json_encode



因此,您应该尝试:

  json_encode($ text,JSON_UNESCAPED_UNICODE); 


I have a PHP script that deals with a wide variety of languages. Unfortunately, whenever I try to use json_encode, any Unicode output is converted to hexadecimal entities. Is this the expected behavior? Is there any way to convert the output to UTF-8 characters?

Here's an example of what I'm seeing:

INPUT

echo $text;

OUTPUT

База данни грешка.

INPUT

json_encode($text);

OUTPUT

"\u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u0438 \u0433\u0440\u0435\u0448\u043a\u0430."

Since PHP/5.4.0, there is an option called "JSON_UNESCAPED_UNICODE". Check it out:

http://se2.php.net/json_encode

Therefore you should try:

json_encode( $text, JSON_UNESCAPED_UNICODE );