PHP - json_encode(string, JSON_UNESCAPED_UNICODE) 不转义捷克字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23322312/
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
PHP - json_encode(string, JSON_UNESCAPED_UNICODE) not escaping czech chars
提问by Jakub Turcovsky
I'm selecting some data from database and encoding them as json, but I've got a problem with czech signs like
我正在从数据库中选择一些数据并将它们编码为 json,但是我遇到了捷克符号的问题,例如
á,í,?,?,?...
á,í,?,?,?...
My file is in utf-8 encoding, my database is also in utf-8 encoding, I've set header to utf-8 encoding as well. What else should I do please?
我的文件是 utf-8 编码,我的数据库也是 utf-8 编码,我也将标头设置为 utf-8 编码。请问我还应该做什么?
My code:
我的代码:
header('Content-Type: text/html; charset=utf-8');
while($tmprow = mysqli_fetch_array($result)) {
$row['user'] = mb_convert_encoding($tmprow['user'], "UTF-8", "auto");
$row['package'] = mb_convert_encoding($tmprow['package'], "UTF-8", "auto");
$row['url'] = mb_convert_encoding($tmprow['url'], "UTF-8", "auto");
$row['rating'] = mb_convert_encoding($tmprow['rating'], "UTF-8", "auto");
array_push($response, $row);
}
$json = json_encode($response, JSON_UNESCAPED_UNICODE);
if(!$json) {
echo "error";
}
and part of the printed json: "package":"zv???tkanalouce"
和部分打印的json: "package":"zv???tkanalouce"
EDIT:Without mb_convert_encoding() function the printed string is empty and "error" is printed.
编辑:如果没有 mb_convert_encoding() 函数,打印的字符串为空并打印“错误”。
回答by hakre
With the code you've got in your example, the output is:
使用示例中的代码,输出为:
json_encode($response, JSON_UNESCAPED_UNICODE);
"package":"zv???tkanalouce"
You see the question marks in there because they have been introduced by mb_convert_encoding
. This happens when you use encoding detection ("auto
" as third parameter) and that encoding detection is not able to handle a character in the input, replacing it with a question mark. Exemplary line of code:
您会在那里看到问号,因为它们是由mb_convert_encoding
. 当您使用编码检测(" auto
" 作为第三个参数)并且编码检测无法处理输入中的字符时,会发生这种情况,将其替换为问号。示例代码行:
$row['url'] = mb_convert_encoding($tmprow['url'], "UTF-8", "auto");
This also means that the data coming out of your database is notUTF-8 encoded because mb_convert_encoding($buffer, 'UTF-8', 'auto');
does not introduce question marks if $buffer
is UTF-8 encoded.
这也意味着来自您的数据库的数据不是UTF-8 编码的,因为mb_convert_encoding($buffer, 'UTF-8', 'auto');
如果$buffer
是 UTF-8 编码,则不会引入问号。
Therefore you need to find out which charset is used in your database connection because the database driver will convert strings into the encoding of the connection.
因此,您需要找出数据库连接中使用的字符集,因为数据库驱动程序会将字符串转换为连接的编码。
Most easy is that you just tell per that database link that you're asking for UTF-8 strings and then just use them:
最简单的是,您只需告诉每个数据库链接您要的是 UTF-8 字符串,然后就可以使用它们:
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
The previous code example just shows how to set the default client character set to UTF-8 with mysqli. It has been taken from the manual, see as well the material we have on site about that, e.g. utf 8 - PHP and MySQLi UTF8.
前面的代码示例仅展示了如何使用 mysqli 将默认客户端字符集设置为 UTF-8。它取自手册,也请参阅我们网站上的相关材料,例如utf 8-PHP 和 MySQLi UTF8。
You can then greatly improve your code:
然后,您可以大大改进您的代码:
$response = $result->fetch_all(MYSQLI_ASSOC);
$json = json_encode($response, JSON_UNESCAPED_UNICODE);
if (FALSE === $json) {
throw new LogicException(
sprintf('Not json: %d - %s', json_last_error(), json_last_error_msg())
);
}
header('Content-Type: application/json');
echo $json;