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 字符串转换为十六进制实体?
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
