This article provides a detailed analysis of the JSON Chinese Garbled text solution in PHP. For more information, see
This article provides a detailed analysis of the JSON Chinese Garbled text solution in PHP. For more information, see
We know that Chinese garbled characters are common when Ajax technology is used to interact with the PHP background. JSON is used as a data exchange format similar to XML, when PHP is used for interaction, text garbled characters may also occur. The solution to php json Chinese garbled characters is similar to the solution to PHP Ajax's Chinese Character garbled characters, the following describes in detail how to solve php json Chinese garbled characters in the form of a tutorial.
Why does Chinese garbled characters appear during PHP interaction with JSON?
As JSON and JS are the same, the client-side characters are processed in the form of UTF8, that is, the characters submitted or returned by JSON are processed in the form of UTF8. when interacting with PHP, if the database encoding and PHP page encoding do not adopt the UTF8 format, Chinese characters will generate garbled characters when PHP interacts with JSON.
Solution to php json Chinese garbled characters
The solution is actually much simpler after you know why PHP interacts with JSON to produce Chinese garbled characters.
Solution to php json Chinese Garbled text Method 1: ensure the consistency of the PHP page encoding in the database and the front and back ends
The simplest way to solve php json Chinese garbled characters is to ensure that the database encoding and PHP page encoding are in the UTF8 format. For beginners of PHP, UTF8 format is preferred for databases, this can reduce the trouble.
Solution to php json Chinese garbled code Method 2: configure the urlencode function of PHP and the decodeURI () function of JS ()
When PHP uses JSON to interact with front-end JS, urlencode must be used for URL encoding before using the json_encode function. In the JS client, decodeURI () is used through the JS function () after URL Decoding, read Chinese information.
Note: When the PHP page interacts with each other through JSON, such as converting an array to JSON format, first, use the urlencode function to encode the URL of the array key and value, and then use the json_encode function and urldecode function.
Method 3 to solve php json Chinese garbled characters: Use the string encoding conversion function
If, for various reasons, the encoding of the database and PHP page are inconsistent, the Hong Kong server, for example, uses GB2312 for the existing database encoding. When JSON interaction is used, you need to use the string encoding conversion function to convert character sets. Common functions include iconv. Because iconv requires the PHP configuration environment support, if not, you can compile the gbk and UTF8 encoding conversion functions to implement encoding conversion.
At this point, three solutions for php json Chinese Character garbled characters have been introduced. In fact, in PHP development, the Hong Kong server encountered PHP-like interaction with AJAX/JS, these methods are basically used to generate Chinese garbled characters. The idea is similar.
, Hong Kong server