为什么PHP json_encode函数将UTF-8字符串转换为十六进制实体? [英] Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

查看:306
本文介绍了为什么PHP json_encode函数将UTF-8字符串转换为十六进制实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本处理各种各样的语言。不幸的是,每当我尝试使用 json_encode ,任何Unicode输出都将转换为十六进制实体。这是预期的行为吗?有没有办法将输出转换为UTF-8字符?



以下是我看到的示例:



INPUT

  echo $ text; 

OUTPUT

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

INPUT

  json_encode($文本); 

OUTPUT

 \\\Б\\\а\\\з\\\а \\\д\\\а\\\н\\\н\\\и \\\г\\\р\\\е\\\ш\\\к\\\а 。 


解决方案

由于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 );

这篇关于为什么PHP json_encode函数将UTF-8字符串转换为十六进制实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆