json_encode():参数中的UTF-8序列无效 [英] json_encode(): Invalid UTF-8 sequence in argument
问题描述
我使用 utf8_general_ci
排序规则对来自MySQL数据库的数据调用 json_encode()
问题是有些行有奇怪的数据,我不能清理。例如符号
,所以一旦它到达 json_encode()
,它会失败, json_encode ):
I'm calling json_encode()
on data that comes from a MySQL database with utf8_general_ci
collation. The problem is that some rows have weird data which I can't clean. For example symbol �
, so once it reaches json_encode()
, it fails with json_encode(): Invalid UTF-8 sequence in argument
.
我尝试了 utf8_encode()
和 utf8_decode()
,即使使用 mb_check_encoding()
,但它仍然通过并造成破坏。
I've tried utf8_encode()
and utf8_decode()
, even with mb_check_encoding()
but it keeps getting through and causing havoc.
在Mac上运行PHP 5.3.10。所以问题是 - 如何清理无效的utf8符号,保留其余的数据,使 json_encoding()
将工作?
Running PHP 5.3.10 on Mac. So the question is - how can I clean up invalid utf8 symbols, keeping the rest of data, so that json_encoding()
would work?
更新。这是一种重现它的方法:
Update. Here is a way to reproduce it:
echo json_encode(pack("H*" ,'c32e'));
推荐答案
看起来像符号 Å
,但由于数据由不应公开的姓氏组成,因此只显示第一个字母,并且只由 $ lastname [0]
,这对于多字节字符串是错误的,并导致整个麻烦。更改为 mb_substr($ lastname,0,1)
- 像一个魅力。
Seems like the symbol was Å
, but since data consists of surnames that shouldn't be public, only first letter was shown and it was done by just $lastname[0]
, which is wrong for multibyte strings and caused the whole hassle. Changed it to mb_substr($lastname, 0, 1)
- works like a charm.
这篇关于json_encode():参数中的UTF-8序列无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!