【发布时间】:2015-05-12 16:25:25
【问题描述】:
我在用 json_encode 编码这个字符时遇到问题
http://www.fileformat.info/info/unicode/char/92/index.htm
首先它给了我这个错误 JSON_ERROR_UTF8 是
'UTF-8 格式错误,可能编码不正确'
所以在json_encode之前尝试了这个函数utf8_encode()
现在返回这个结果'\u0092'
所以我找到了这个
function jsonRemoveUnicodeSequences($struct) {
return preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($struct));
}
角色出现了,但与其他角色一起出现
Â’
也试过htmlentities然后html_entity_decode
没有结果
【问题讨论】:
-
你的输入编码是什么?
json_encode之前可以转成utf8吗? -
@Halcyon 我的输入是对象我使用这个函数进行 utf8 编码函数 utf8ize($mixed) { if (is_array($mixed) ) { foreach ($mixed as $key => $value) { $mixed[$key] = utf8ize($value); } } else if (is_object($mixed)) { foreach ($mixed as $key => $value) { $mixed->$key = utf8ize($value); } } else if (is_string ($mixed)) { return utf8_encode($mixed); } 返回 $mixed; }
-
为什么不直接
json_encode(iconv('UCS-4LE','UTF-8', $text))? -
它的创建错误“检测到输入字符串中的不完整多字节字符”导致我看到这篇文章stackoverflow.com/questions/26092388/…,它具有我一直在寻找的功能
-
如果在这里找到有用的功能stackoverflow.com/a/29667430/3479609