联系电话:020-28187900

json_encode对无效utf-8谷歌chrome下解析出错

发表时间:2012-08-14 阅读: 6913

php的json_encode错误收集。

在firefox,IE下都是正常的通信,但是在Chrome中却运行不畅,提示“json_encode() Invalid UTF-8 sequence in argument”类错误提示。

这是json_encode对无效utf-8谷歌chrome下解析出错,如果php开启了错误显示配置:display_error=On,json_encode 遇到无效utf-8字符时,直接返回null,不会抛出任何错误;如果关闭了错误显示:display_error=Off,json_encode遇到无效utf-8编码时抛出warnning。

配置不了php.ini,就需要在程序当中临时替换设置,即可避免此bug。

该php Bugs记录如下:

[2010-07-22 01:00 UTC] david at grudl dot com

Description:
------------
Invalid UTF-8 string throws E_WARNING depending on the configuration directive display_errors:

ini_set('display_errors', 0);
json_encode("xFF");  // PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument (in error log)
ini_set('display_errors', 1);
json_encode("xFF");  // *nothing*

This directive determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user. It should not be used to change json_encode behaviour.

The same bug affects htmlentities() and htmlspecialchars()

Test script:
---------------
ini_set('display_errors', 0);
json_encode("xFF");
htmlspecialchars("xFF", ENT_COMPAT, 'utf-8');
htmlentities("xFF", ENT_COMPAT, 'utf-8');

参考资料:php.netI am MT


相关文章 【更多】

唯众网络