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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16498286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:13:41  来源:igfitidea点击:

Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

phpencodingjson

提问by David Jones

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?

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

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."

回答by John Severinson

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

从 PHP/5.4.0 开始,有一个名为"JSON_UNESCAPED_UNICODE". 一探究竟:

http://se2.php.net/json_encode

http://se2.php.net/json_encode

Therefore you should try:

因此,您应该尝试:

json_encode( $text, JSON_UNESCAPED_UNICODE );

回答by mpyw

JSON_UNESCAPED_UNICODE is available on PHP Version 5.4 or later.
The following code is for Version 5.3.

JSON_UNESCAPED_UNICODE 在 PHP 5.4 或更高版本上可用。
以下代码适用于 5.3 版。

UPDATED

更新

  • html_entity_decodeis a bit more efficient than pack+ mb_convert_encoding.
  • (*SKIP)(*FAIL)skips backslashes itself and specified characters by JSON_HEX_*flags.
  • html_entity_decodepack+效率更高一些mb_convert_encoding
  • (*SKIP)(*FAIL)跳过反斜杠本身和JSON_HEX_*标志指定的字符。

 

 

function raw_json_encode($input, $flags = 0) {
    $fails = implode('|', array_filter(array(
        '\\',
        $flags & JSON_HEX_TAG ? 'u003[CE]' : '',
        $flags & JSON_HEX_AMP ? 'u0026' : '',
        $flags & JSON_HEX_APOS ? 'u0027' : '',
        $flags & JSON_HEX_QUOT ? 'u0022' : '',
    )));
    $pattern = "/\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
    $callback = function ($m) {
        return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
    };
    return preg_replace_callback($pattern, $callback, json_encode($input, $flags));
}

回答by Adrian Romero

You like to set charset and unescaped unicode

您喜欢设置字符集和未转义的 unicode

 header('Content-Type: application/json;charset=utf-8');  
 json_encode($data,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

回答by Steffo Dimfelt

One solution is to first encode data and then decode it in the same file:

一种解决方案是先对数据进行编码,然后在同一个文件中对其进行解码:

$string =json_encode($input, JSON_UNESCAPED_UNICODE) ; 
echo $decoded = html_entity_decode( $string );

回答by gaba

Here is my combined solution for various PHP versions.

这是我针对各种 PHP 版本的组合解决方案。

In my company we are working with different servers with various PHP versions, so I had to find solution working for all.

在我的公司,我们正在使用具有各种 PHP 版本的不同服务器,因此我必须找到适合所有人的解决方案。

$phpVersion = substr(phpversion(), 0, 3)*1;

if($phpVersion >= 5.4) {
  $encodedValue = json_encode($value, JSON_UNESCAPED_UNICODE);
} else {
  $encodedValue = preg_replace('/\\u([a-f0-9]{4})/e', "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U')))", json_encode($value));
}

Credits should go to Marco Gasi& abu. The solution for PHP >= 5.4 is provided in the json_encode docs.

学分应该去Marco Gasi& abu。PHP >= 5.4 的解决方案在 json_encode 文档中提供。

回答by abu

The raw_json_encode() function abovedid not solve me the problem (for some reason, the callback function raised an error on my PHP 5.2.5 server).

该raw_json_encode()函数上面并没有解决我的问题(由于某种原因,回调函数提出了我的PHP 5.2.5服务器上的错误)。

But this other solution did actually work.

但是这个其他解决方案确实有效。

https://www.experts-exchange.com/questions/28628085/json-encode-fails-with-special-characters.html

https://www.experts-exchange.com/questions/28628085/json-encode-fails-with-special-characters.html

Credits should go to Marco Gasi. I just call his function instead of calling json_encode():

学分应该去Marco Gasi。我只是调用他的函数而不是调用 json_encode():

function jsonRemoveUnicodeSequences( $json_struct )
{ 
    return preg_replace( "/\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U')))", json_encode( $json_struct ) );
}

回答by Hoàng V? Tgtt

json_encode($text, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

回答by Robin Carlo Catacutan

Since you asked:

既然你问:

Is there any way to convert the output to UTF-8 characters?

有没有办法将输出转换为 UTF-8 字符?

Another solution is to use utf8_encode.

另一种解决方案是使用utf8_encode

This will encode your string to UTF-8.

这会将您的字符串编码为UTF-8.

e.g.

例如

foreach ($rows as $key => $row) {
  $rows[$key]["keyword"] = utf8_encode($row["keyword"]);
}

echo json_encode($rows);

回答by JoakimH

Is this the expected behavior?

这是预期的行为吗?

the json_encode()only works with UTF-8 encoded data.

json_encode()只适用于UTF-8编码的数据。

maybe you can get an answer to convert it here: cyrillic-characters-in-phps-json-encode

也许您可以在此处获得转换的答案:cyrillic-characters-in-phps-json-encode