Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?(为什么PHP json_encode 函数会将UTF-8 字符串转换为十六进制实体?)
问题描述
我有一个处理多种语言的 PHP 脚本.不幸的是,每当我尝试使用 json_encode
时,任何 Unicode 输出都会转换为十六进制实体.这是预期的行为吗?有没有办法将输出转换为UTF-8字符?
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:
输入
echo $text;
输出
База данни грешка.
输入
json_encode($text);
输出
"u0411u0430u0437u0430 u0434u0430u043du043du0438 u0433u0440u0435u0448u043au0430."
推荐答案
自 PHP/5.4.0 起,有一个名为 JSON_UNESCAPED_UNICODE
的选项.看看:
Since PHP/5.4.0, there is an option called JSON_UNESCAPED_UNICODE
. Check it out:
https://php.net/function.json-encode
因此您应该尝试:
json_encode( $text, JSON_UNESCAPED_UNICODE );
这篇关于为什么PHP json_encode 函数会将UTF-8 字符串转换为十六进制实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么PHP json_encode 函数会将UTF-8 字符串转换为十六进制实体?


- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01