Home  >  Article  >  Backend Development  >  How to solve the problem of Chinese garbled characters when php outputs json

How to solve the problem of Chinese garbled characters when php outputs json

PHPz
PHPzOriginal
2023-04-25 17:37:402025browse

In recent years, with the rapid development of Internet technology, the development model that separates the front and back ends has become the choice of more and more developers. There are more and more problems coming, and one of the more common problems is the problem of garbled characters when outputting json format data on the backend, especially when using PHP as the backend language.

1. Problem description

When using PHP to output json format data, the problem of Chinese garbled characters often occurs. For example, we use the following code:

<?php

header(&#39;Content-type: application/json; charset=UTF-8&#39;);

$data = array(&#39;name&#39; => '张三', 'age' => '20');

echo json_encode($data);

?>

We expect that the output result should be:

{"name": "张三", "age": "20"}

But in fact, we will see that the Chinese part of the output result becomes a bunch of garbled characters:

{"name": "\u5f20\u4e09", "age": "20"}

2. Problem Analysis

Why does this kind of garbled code appear? This is caused by the processing rules for Chinese characters in json.

In json, non-English characters will be escaped using Unicode escape sequences. Specifically, characters are represented by \u plus the corresponding Unicode encoding.

For example, the character "Zhang", its Unicode encoding is "U 5F20", that is, "\u5F20".

Therefore, when we use json_encode to encode an array in php, json_encode will escape the Chinese characters into the form of Unicode escape sequence, that is, "\u plus the corresponding Unicode encoding" .

If the correct character set is not specified when outputting json data, the browser will parse according to the default character set when parsing, resulting in Chinese garbled characters.

3. Solution

If we understand the cause of Chinese garbled characters, we can use corresponding solutions to deal with this problem.

Commonly used solutions include the following:

  1. Specify the character set in the header

We can set the header before outputting json data information to specify the correct character set. Taking UTF-8 as an example, you can use the following code:

header('Content-type: application/json; charset=UTF-8');
  1. Set the parameters in the json_encode function

We can set the parameters in the json_encode function Add a parameter to indicate the character set used. Taking UTF-8 as an example, you can use the following code:

echo json_encode($data, JSON_UNESCAPED_UNICODE);
  1. Set the php default character set in php.ini

We can also modify the configuration in php.ini and set the default character set to UTF-8 to ensure that the correct character set is used when outputting json data.

default_charset = 'UTF-8'

In short, no matter which method is used, you need to ensure that the correct character set is used when outputting json data, so as to avoid the problem of Chinese garbled characters.

4. Summary

When using PHP to output json data, if you encounter the problem of Chinese garbled characters, you need to check the following aspects:

  1. Whether the settings are correct The character set in the header information;
  2. Whether the character set parameter in the json_encode function is correctly set;
  3. Whether the default character set in php.ini is correctly set;

Only by ensuring that the character set is correctly set in all aspects can we effectively avoid the problem of Chinese garbled characters, so that the output json data can be parsed normally.

The above is the detailed content of How to solve the problem of Chinese garbled characters when php outputs json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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