What to do if php json decode Chinese garbled characters

藏色散人
Release: 2023-03-02 17:24:01
Original
4840 people have browsed it

php json decode garbled solution: first, before JSON processing, convert the characters into UTF8 form; then use "json_encode" in the background PHP page to convert the array array in PHP into a JSON string. Can.

What to do if php json decode Chinese garbled characters

PHP json_encode Chinese garbled solution

I believe many people use Ajax to interact with the background php page I have all encountered the problem of garbled Chinese characters. As a lightweight data exchange format, JSON is very popular. However, using PHP as the backend interaction is prone to the problem of Chinese garbled characters.

JSON is the same as js. Client characters are processed in the form of UTF8. That is to say, when using JSON as the data format for submission and reception, the characters are processed by UTF8 encoding. When our page When encoding and database encoding do not use UTF8, it is extremely easy for Chinese garbled characters to appear. The solution is naturally to use UTF8 when processing JSON data with js or PHP.

PHP5.2 or above uses json_encode as a built-in function, which brings great convenience to website makers. However, we must note that json_encode only supports UTF8 encoding. characters, otherwise, Chinese garbled characters or null values will appear.

The solution is divided into the following two steps.

Step1

Ensure that characters are encoded in UTF8 when processing JSON. Specifically, we can change both the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the characters to UTF8 before JSON processing. There are the following methods in PHP:

Copy after login

Step2

Background PHP page (page Encoded to UTF-8 or the characters have been converted to UTF-8) Use json_encode to convert the array array in PHP into a JSON string. For example:

'中文字符串','value'=>'test'); echo json_encode($testJSON); ?>
Copy after login

View the output result as:

{“name”:”\u4e2d\u6587\u5b57\u7b26\u4e32″,”value”:”test”}
Copy after login

It can be seen that even if UTF8-encoded characters are used, Chinese garbled characters appear when using json_encode. The solution is to use the urlencode() function to process the characters before using json_encode, then json_encode them, and use the urldecode() function to convert them back when outputting the results. The details are as follows:

'中文字符串','value'=>'test'); //echo json_encode($testJSON); foreach ( $testJSON as $key => $value ) { $testJSON[$key] = urlencode ( $value ); } echo urldecode ( json_encode ( $testJSON ) ); ?>
Copy after login

Check the output result as:

{“name”:”中文字符串”,”value”:”test”}
Copy after login

At this point, Chinese characters are successfully output. Feel free to use json_encode. In this way, the JSON string output in the PHP background will not appear Chinese garbled when eval is received by Ajax in the front-end javascript, because js also processes JSON format data in the form of UTF8, which is similar to PHP, so it receives the PHP page The JSON string does not cause problems.

Recommended: "PHP Tutorial"

The above is the detailed content of What to do if php json decode Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!