php json_encode(): 参数中的 UTF-8 序列无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10205722/
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
json_encode(): Invalid UTF-8 sequence in argument
提问by Artjom Kurapov
I'm calling json_encode()on data that comes from a MySQL database with utf8_general_cicollation. 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.
我正在调用json_encode()来自带有utf8_general_ci排序规则的 MySQL 数据库的数据。问题是有些行有我无法清理的奇怪数据。例如符号?,所以一旦到达json_encode(),它就会失败json_encode(): Invalid UTF-8 sequence in argument。
I've tried utf8_encode()and utf8_decode(), even with mb_check_encoding()but it keeps getting through and causing havoc.
我试过utf8_encode()和utf8_decode(),甚至mb_check_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?
在 Mac 上运行 PHP 5.3.10。所以问题是 - 我如何清理无效的 utf8 符号,保留其余数据,这样json_encoding()才能工作?
Update. Here is a way to reproduce it:
更新。这是一种重现它的方法:
echo json_encode(pack("H*" ,'c32e'));
采纳答案by Artjom Kurapov
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.
似乎符号是?,但是由于数据由不应公开的姓氏组成,因此只显示了第一个字母并且它由 just 完成$lastname[0],这对于多字节字符串是错误的并导致整个麻烦。将其更改为mb_substr($lastname, 0, 1)- 就像一个魅力。
回答by Robert Imhoff
I had a similar error which caused json_encode to return a null field whenever there was a hi-ascii character such as a curly apostrophe in a string, due to the wrong character set being returned in the query.
由于查询中返回了错误的字符集,我遇到了类似的错误,导致 json_encode 返回空字段,只要字符串中有一个 hi-ascii 字符,例如卷曲撇号。
The solution was to make sure it comes as utf8 by adding:
解决方案是通过添加以下内容来确保它以 utf8 格式出现:
mysql_set_charset('utf8');
after the mysql connect statement.
在 mysql 连接语句之后。
回答by serge.k
The problem is that this character is UTF8, but json_encode does not handle it correctly. To say more, there is a list of other characters (see Unicode characters list), that will trigger the same error, so stripping off this one (?) will not correct an issue to the end.
问题是这个字符是 UTF8,但是 json_encode 没有正确处理它。多说一点,还有一个其他字符的列表(请参阅Unicode 字符列表),这将触发相同的错误,因此剥离这个 (?) 并不能解决问题到底。
What we have used is to convert these chars to html entities like this:
我们使用的是将这些字符转换为 html 实体,如下所示:
htmlentities( (string) $value, ENT_QUOTES, 'utf-8', FALSE);
回答by Emil Vikstr?m
Make sure that your connection charset to MySQL is UTF-8. It often defaults to ISO-8859-1 which means that the MySQL driver will convert the text to ISO-8859-1.
确保您到 MySQL 的连接字符集是 UTF-8。它通常默认为 ISO-8859-1,这意味着 MySQL 驱动程序会将文本转换为 ISO-8859-1。
You can set the connection charset with mysql_set_charset, mysqli_set_charsetor with the query SET NAMES 'utf-8'
您可以使用mysql_set_charset、mysqli_set_charset或查询设置连接字符集SET NAMES 'utf-8'
回答by Can Uluda?
Using this code might help. It solved my problem!
使用此代码可能会有所帮助。它解决了我的问题!
mb_convert_encoding($post["post"],'UTF-8','UTF-8');
or like that
或者像那样
mb_convert_encoding($string,'UTF-8','UTF-8');
回答by Evert
The symbol you posted is the placeholder symbol for a broken byte sequence. Basically, it's not a real symbol but an error in your string.
您发布的符号是损坏的字节序列的占位符符号。基本上,它不是真正的符号,而是字符串中的错误。
What is the exact byte value of the symbol? Blindly applying utf8_encode is not a good idea, it's better to find out first where the byte(s) came from and what they mean.
符号的确切字节值是多少?盲目应用 utf8_encode 不是一个好主意,最好先找出字节的来源以及它们的含义。
回答by rharvey
Another thing that throws this error, when you use php's json_encode function, is when unicode characters are upper case \U and not lower case \u
引发此错误的另一件事是,当您使用 php 的 json_encode 函数时,unicode 字符是大写 \U 而不是小写 \u
回答by Jamie Deakin
Updated.. I solved this issue by stating the charset on PDO connection as below:
更新..我通过说明 PDO 连接上的字符集解决了这个问题,如下所示:
"mysql:host=$host;dbname=$db;charset=utf8"
"mysql:host=$host;dbname=$db;charset=utf8"
All data received was then in the correct charset for the rest of the code to use
然后所有收到的数据都在正确的字符集中,供其余代码使用
回答by inrsaurabh
I am very late but if some one working on SLIM to make rest api and getting same error can solve this problem by adding below line as:
<?php
// DbConnect.php file
class DbConnect
{
//Variable to store database link
private $con;
//Class constructor
function __construct()
{
}
//This method will connect to the database
function connect()
{
//Including the constants.php file to get the database constants
include_once dirname(__FILE__) . '/Constants.php';
//connecting to mysql database
$this->con = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
mysqli_set_charset($this->con, "utf8"); // add this line
//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//finally returning the connection link
return $this->con;
}
}
回答by Deepika Patel
json_encode works only with UTF-8 data. You'll have to ensure that your data is in UTF-8. alternatively, you can use iconv() to convert your results to UTF-8 before feeding them to json_encode()
json_encode 仅适用于 UTF-8 数据。您必须确保您的数据采用 UTF-8 格式。或者,您可以使用 iconv() 将结果转换为 UTF-8,然后再将它们提供给 json_encode()

