PHP json_encode 中的西里尔字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/410704/
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-24 22:40:02  来源:igfitidea点击:

Cyrillic characters in PHP's json_encode

phputf-8json

提问by AquilaX

I'm trying to encode Cyrillic UTF-8 array to JSON string using php's function json_encode. The sample code looks like this:

我正在尝试使用 php 的函数 json_encode 将 Cyrillic UTF-8 数组编码为 JSON 字符串。示例代码如下所示:

<?php
  $arr = array(
     'едно' => 'първи',
     'две' => 'втори'
  );
  $str = json_encode($arr);
  echo $str;
?>

It works fine but the result of the script is represented as:

它工作正常,但脚本的结果表示为:

{"\u0435\u0434\u043d\u043e":"\u043f\u044a\u0440\u0432\u0438","\u0434\u0432\u0435":"\u0432\u0442\u043e\u0440\u0438"}

which makes 6 characters for each Cyrillic character. Is there a way to get the original characters for key/value pairs instead of encoded ones?

这为每个西里尔字符生成 6 个字符。有没有办法获取键/值对的原始字符而不是编码的字符?

回答by Alexander Farber

Can't you use JSON_UNESCAPED_UNICODEconstant here?

你不能在这里使用JSON_UNESCAPED_UNICODE常量吗?

回答by Boris Chervenkov

I found this in the code of Zend framework:

我在 Zend 框架的代码中发现了这一点:

http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php

http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php

Take a look at the function decodeUnicodeString ( line 474 ):

看看函数 decodeUnicodeString (第 474 行):

 /**
     * Decode Unicode Characters from \u0000 ASCII syntax.
     *
     * This algorithm was originally developed for the
     * Solar Framework by Paul M. Jones
     *
     * @link   http://solarphp.com/
     * @link   http://svn.solarphp.com/core/trunk/Solar/Json.php
     * @param  string $value
     * @return string
     */
    public static function decodeUnicodeString($chrs)

It's static, and you can easily extract it - just replace the line:

它是静态的,您可以轻松提取它 - 只需替换该行:

490:           $utf8 .= self::_utf162utf8($utf16);

with:

和:

490:           $utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');

Not an ideal solution, but did the job for me :o)

不是理想的解决方案,但为我完成了工作:o)

回答by chanklor

$str = json_encode($arr, JSON_UNESCAPED_UNICODE);

The use of this solution worked for me with the Latin and the Cyrillic alphabet, with PHP 5.5

这个解决方案的使用对我来说适用于拉丁字母和西里尔字母,以及 PHP 5.5

回答by cesar

This is a realy old question, but I don't think it was answered correctly.

这是一个非常古老的问题,但我认为没有正确回答。

use something like this:

使用这样的东西:

print json_encode($array, JSON_UNESCAPED_UNICODE);

回答by Beau Simensen

It looks like PHP's built in json_encodeonly works with UTF-8, and no other bells and whistles for tweaking how it works with respect to encoding.

看起来 PHP 的内置json_encode只适用于UTF-8,没有其他花里胡哨的东西来调整它在编码方面的工作方式。

I found A completely fair and balanced comparison of php json librarieson Google. It might help you. You might try another library based on the tables here, if possible. There are additional PHP libraries listed at json.orgthat you can experiment with.

我在 Google 上找到了一个完全公平和平衡的 php json 库比较。它可能会帮助你。如果可能,您可以根据此处的表格尝试另一个库。json.org上列出了其他 PHP 库,您可以进行试验。

回答by AquilaX

It worked with http://pear.php.net/pepr/pepr-proposal-show.php?id=198

它适用于http://pear.php.net/pepr/pepr-proposal-show.php?id=198

With nasty bypass in JSON.php, rows 298..

在 JSON.php 中有讨厌的绕过,第 298 行..

$char = pack('C*', $ord_var_c, ord($var{$c + 1}));
$c += 1;
//$utf16 = $this->utf82utf16($char);
//$ascii .= sprintf('\u%04s', bin2hex($utf16));
$ascii .= $char;

Thanks!

谢谢!

回答by Erdin? ?orbac?

I was dealing the same problem for Turkish ... indeed we don't have to do anything browsers automatically converts them in JS code blocks. So the easiest way of getting them decoded is getting them through javascript. (Ajax etc...)

我正在为土耳其语处理同样的问题……事实上,我们不必做任何事情,浏览器会自动将它们转换为 JS 代码块。因此,解码它们的最简单方法是通过 javascript 获取它们。(阿贾克斯等...)

Json encode for with non ascii characters ?

Json 编码为非 ascii 字符?