PHP JSON Chinese garbled solution solution_PHP tutorial

WBOY
Release: 2016-07-14 10:06:35
Original
826 people have browsed it

We know that when using Ajax technology to interact with the PHP backend, Chinese garbled characters are common. JSON, as a data exchange format similar to XML, will also cause Chinese garbled characters when PHP is used to interact. Solve PHP The idea of ​​solving Chinese garbled characters in JSON is actually similar to the solution to garbled Chinese characters in PHP Ajax values. Below, I will introduce in detail the method of solving Chinese garbled characters in PHP JSON in the form of a tutorial.

Why do Chinese garbled characters appear when PHP interacts with JSON?

Since JSON is the same as JS, the characters on the client are processed in the form of UTF8, that is, the characters submitted or received by JSON are processed in the form of UTF8. When interacting with PHP, if the database encoding and PHP page encoding do not use UTF8 format, Chinese characters will be garbled when PHP interacts with JSON.

How to solve Chinese garbled characters in PHP JSON

After knowing the reason why Chinese garbled characters are generated when PHP interacts with JSON, the solution is actually much simpler.

Method 1 to solve PHP JSON Chinese garbled code: Ensure that the encoding of the database, front-end and back-end PHP pages is consistent

The simplest way to solve the Chinese garbled PHP JSON is to ensure that the database encoding and PHP page encoding are uniformly in UTF8 format. For beginners of PHP, it is best to use UTF8 format for the database when building a website, which can reduce a lot of trouble.

Method 2 to solve PHP JSON Chinese garbled code: configure and use PHP function urlencode and JS function decodeURI()

When PHP uses JSON to interact with the front-end JS, you need to use the urlencode function to encode the URL before using the json_encode function. On the JS client, use the JS function decodeURI() to decode the URL and then read the Chinese information.

Note: When interacting through JSON in a PHP page, such as converting an array into JSON format, first use the urlencode function to URL-encode the array key and value, and then use the json_encode function and urldecode function.

Method three to solve PHP JSON Chinese garbled code: Use string encoding conversion function

When the database encoding and PHP page encoding are inconsistent due to various reasons, for example, the existing database encoding uses GB2312, then when using JSON interaction, you need to use the string encoding conversion function to convert between character sets. . Commonly used functions include iconv. Since iconv requires PHP configuration environment support, if it is not supported, you can implement encoding conversion by writing your own gbk and UTF8 encoding conversion functions.

At this point, the three solutions to garbled Chinese characters in PHP JSON have been introduced. In fact, in PHP development, these methods are basically used when encountering the interaction between PHP and AJAX/JS and generating Chinese garbled characters. The idea is similar of.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477937.htmlTechArticleWe know that when using Ajax technology to interact with the PHP background, Chinese garbled characters are common. JSON is as different as XML Similar data exchange format, Chinese garbled characters will also appear when PHP is used for interaction...
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