Home > Backend Development > PHP Tutorial > What should I do if JSON encoding and decoding in PHP encounters Chinese garbled characters?

What should I do if JSON encoding and decoding in PHP encounters Chinese garbled characters?

王林
Release: 2024-03-08 11:02:01
Original
575 people have browsed it

What should I do if JSON encoding and decoding in PHP encounters Chinese garbled characters?

When using JSON encoding and decoding in PHP, encountering Chinese garbled characters is a common problem. This is usually caused by inconsistent character encoding between server and client. The following will introduce in detail the solution when encountering JSON encoding and decoding Chinese garbled characters in PHP, and provide specific code examples.

First of all, it should be clear that the json_encode() function in PHP will escape Chinese to Unicode encoding by default, and the json_decode() function will automatically process it when parsing. However, if Chinese garbled characters appear during use, you can try the following solutions.

Solution 1: Set character encoding

In PHP, you can use the header() function to set the character encoding in the HTTP header information to ensure that the server and client use unified Character Encoding. This can avoid the problem of Chinese garbled characters. The following is a simple sample code:

header('Content-Type: application/json; charset=utf-8');

$data = array(
    'name' => '张三',
    'age' => 25,
    'city' => '北京'
);

$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo $json;
Copy after login

In the above example, when setting the Content-Type, we also specified the character encoding as utf-8, and then used the json_encode() function to convert data containing Chinese Convert to JSON format and use the JSON_UNESCAPED_UNICODE parameter to avoid escaping Chinese characters into Unicode encoding.

Solution 2: Manual conversion of encoding

If the consistency of character encoding cannot be guaranteed between the server and the client, manual conversion can be performed during the encoding and decoding process. The following is a sample code:

$data = array(
    'name' => '李四',
    'age' => 30,
    'city' => '上海'
);

// 编码时手动转换编码
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
$json = mb_convert_encoding($json, 'UTF-8', 'UTF-8');

// 解码时手动转换编码
$json = mb_convert_encoding($json, 'UTF-8', 'UTF-8');
$data = json_decode($json, true);

var_dump($data);
Copy after login

In the above code, we use the mb_convert_encoding() function to manually convert the encoding to UTF-8 to ensure the consistency of the encoding.

Conclusion

Whether it is by setting the character encoding or manually converting the encoding, it can help solve the problem of Chinese garbled characters encountered in JSON encoding and decoding in PHP. Of course, it is necessary to ensure the consistency of character encoding between the server and the client to avoid Chinese garbled characters.

Hope the above content can help you solve the problem of JSON encoding and decoding Chinese garbled characters in PHP. If you have any questions or other issues, please leave a message for discussion.

The above is the detailed content of What should I do if JSON encoding and decoding in PHP encounters Chinese garbled characters?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template