json_encode()非utf-8字符串? [英] json_encode() non utf-8 strings?

查看:191
本文介绍了json_encode()非utf-8字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字符串数组,所有的字符串都使用系统默认的ANSI编码,并从sql数据库中提取。所以有256种不同的可能字符字节值(单字节编码)。有没有办法让json_encode()工作并显示这些字符,而不必在我的所有字符串上使用utf8_encode(),并以\\\‚这样的东西结束?或者是json的标准吗?

So I have an array of strings, and all of the strings are using the system default ANSI encoding and were pulled from a sql database. So there are 256 different possible character byte values (single byte encoding). Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like "\u0082"?

推荐答案


有没有办法可以让json_encode()工作并显示这些字符而不是必须在我的所有字符串上使用utf8_encode(),并以\\\‚这样的东西结束?

Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like "\u0082"?

如果你有ANSI编码的字符串,使用 utf8_encode()是处理此问题的错误函数。您需要首先将其从ANSI转换为UTF-8。这肯定会从json输出中减少Unicode转义序列的数量,如 \\\‚ ,但从技术上讲,这些序列对json有效,你不要害怕。

If you have an ANSI encoded string, using utf8_encode() is the wrong function to deal with this. You need to properly convert it from ANSI to UTF-8 first. That will certainly reduce the number of Unicode escape sequences like \u0082 from the json output, but technically these sequences are valid for json, you must not fear them.

json_encode UTF-8 编码字符串 配合使用。如果您需要从 ANSI 编码字符串中成功创建有效的 json ,则需要重新编码/转换为 UTF-8 然后 json_encode 将正常工作。

json_encode works with UTF-8 encoded strings only. If you need to create valid json successfully from an ANSI encoded string, you need to re-encode/convert it to UTF-8 first. Then json_encode will just work as documented.

要转换 ANSI (更正确地,我假设你有一个 Windows-1252 编码字符串,这是受欢迎但错误地称为$ code> ANSI )到 UTF-8 可以使用 mb_convert_encoding() 功能:

To convert an encoding from ANSI (more correctly I assume you have a Windows-1252 encoded string, which is popular but wrongly referred to as ANSI) to UTF-8 you can make use of the mb_convert_encoding() function:

$str = mb_convert_encoding($str, "UTF-8", "Windows-1252");

可以转换字符串的编码/字符集的PHP中的另一个函数称为 iconv 根据 libiconv 。您也可以使用它:

Another function in PHP that can convert the encoding / charset of a string is called iconv based on libiconv. You can use it as well:

$str = iconv("CP1252", "UTF-8", $str);



关于utf8_encode()的注释



< a href =http://php.net/manual/en/function.utf8-encode.php =nofollow noreferrer> utf8_encode() 只适用于 Latin-1 ,而不适用于 ANSI 。所以当您通过该函数运行时,您将会损坏字符串中的部分字符。

Note on utf8_encode()

utf8_encode() does only work for Latin-1, not for ANSI. So you will destroy part of your characters inside that string when you run it through that function.

相关:什么是ANSI格式?

对于 json_encode()返回的更精细的控件,请参阅预定义常量列表(PHP版本依赖,包括PHP 5.4,一些常量保持不记录,仅在源代码中可用

For a more fine-grained control of what json_encode() returns, see the list of predifined constants (PHP version dependent, incl. PHP 5.4, some constants remain undocumented and are available in the source code only so far).

正如您在评论您有将应用程序应用于数组的问题,这里是一些代码示例。在使用 json_encode 之前,首先需要 更改编码。这只是一个标准的数组操作,对于简单的 pdo :: fetch() a foreach iteration:

As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's always needed to first change the encoding before using json_encode. That's just a standard array operation, for the simpler case of pdo::fetch() a foreach iteration:

while($row = $q->fetch(PDO::FETCH_ASSOC))
{
  foreach($row as &$value)
  {
    $value = mb_convert_encoding($value, "UTF-8", "Windows-1252");
  }
  unset($value); # safety: remove reference
  $items[] = array_map('utf8_encode', $row );
}

这篇关于json_encode()非utf-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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