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, chinese garbled characters also occur when PHP is used for interaction. we know that Chinese garbled characters are common when Ajax technology is used to interact with the PHP background, JSON is a data exchange format similar to XML. Chinese characters are garbled during PHP interaction, the solution to php json Chinese garbled characters is similar to the solution to PHP Ajax's Chinese character garbled characters. below I will introduce in detail the solution to 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, you need to use json_encode before using the json_encode function.Urlencode functionURL encoding.JS function decodeURI ()After URL decoding, read Chinese information.
Note:: When using JSON for interaction on the PHP page, for example, converting an array to JSON format, useUrlencode functionEncode 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 database encoding and PHP page encoding are inconsistent, for example, the existing database encoding uses GB2312, when JSON interaction is used, you need to use the string encoding conversion function to convert character sets. Iconv is a commonly used function. because iconv requires the PHP configuration environment support, if not, you can compile the gbk and UTF8 encoding conversion functions to implement encoding conversion, for details, refer to PHP Ajax to solve Chinese character garbled characters.
So far, three solutions for php json Chinese character garbled characters have been introduced. In fact, in PHP Development, PHP interacts with AJAX/JS, these methods are basically used to generate Chinese garbled characters. The idea is similar.