PHP - json_encode(string,JSON_UNESCAPED_UNICODE)不转义捷克字符 [英] PHP - json_encode(string, JSON_UNESCAPED_UNICODE) not escaping czech chars

查看:238
本文介绍了PHP - json_encode(string,JSON_UNESCAPED_UNICODE)不转义捷克字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中选择一些数据并将其编码为json,但是我遇到了一些问题,例如:

I'm selecting some data from database and encoding them as json, but I've got a problem with czech signs like


á,í,ř,č,ž...

á,í,ř,č,ž...

我的文件是utf-8编码,我的数据库也在utf -8编码,我也设置头到utf-8编码。我还应该怎么办?

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?

我的代码:

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";
    }

和部分打印的json:package :zv?tkanalouce

and part of the printed json: "package":"zv???tkanalouce"

编辑:没有mb_convert_encoding()函数,打印的字符串为空,

Without mb_convert_encoding() function the printed string is empty and "error" is printed.

推荐答案

使用您的示例中的代码,输出为:

With the code you've got in your example, the output is:

json_encode($response, JSON_UNESCAPED_UNICODE);
"package":"zv???tkanalouce"

你看到问号因为它们是由 mb_convert_encoding 引入的。当您使用编码检测( auto 作为第三个参数)时,会发生这种情况,编码检测不能处理输入中的字符,用问号替换。示例代码行:

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:

$row['url'] = mb_convert_encoding($tmprow['url'], "UTF-8", "auto");

这也意味着数据库中出现的数据不是 UTF -8编码,因为 mb_convert_encoding($ buffer,'UTF-8','auto'); 不会引入问号,如果 $ buffer 是UTF-8编码。

This also means that the data coming out of your database is not UTF-8 encoded because mb_convert_encoding($buffer, 'UTF-8', 'auto'); does not introduce question marks if $buffer is UTF-8 encoded.

因此,您需要找出数据库连接中使用的字符集,因为数据库驱动程序将字符串转换为连接。

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.

最容易的是,您只需要查询每个数据库链接,您要求的是UTF-8字符串,然后使用它们:

Most easy is that you just tell per that database link that you're asking for UTF-8 strings and then just use them:

$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());
}

以前的代码示例仅显示如何将默认客户端字符集设置为UTF -8与mysqli。已经取自手册,另见我们的材料有现场的,例如 utf 8 - PHP和MySQLi UTF8

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.

然后你可以大大提高你的代码:

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;

这篇关于PHP - json_encode(string,JSON_UNESCAPED_UNICODE)不转义捷克字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆