Home > Article > Backend Development > How to solve garbled Chinese characters in php json
php json Chinese character garbled solution: 1. Ensure that the encoding of the database and the front and backend php pages are consistent; 2. Use the urlencode and decodeURL functions to perform URL encoding; 3. Use the string encoding function to perform encoding between character sets Convert.
#Why do Chinese garbled characters appear when PHP interacts with JSON?
(Recommended tutorial: php video tutorial)
Since JSON is the same as JS, the characters on the client are processed in the form of UTF8, that is, JSON is submitted or accepted and returned Characters are processed in UTF8 format. 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.
Solution:
After knowing the reason why the interaction between PHP and JSON produces Chinese garbled characters, the solution is actually much simpler.
Method 1 to solve the Chinese garbled code in PHP JSON: Ensure that the encoding of the database, front and backend PHP pages is consistent
The simplest way to solve the Chinese garbled PHP JSON is to ensure the database encoding , PHP page encoding uniformly uses 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.
Solution to PHP JSON Chinese garbled method two: Configure and use the PHP function urlencode and JS function decodeURI()
When PHP uses JSON to interact with the front-end JS, use json_encode Before the function, you need to use the urlencode function to encode the URL. The JS client uses the JS function decodeURI() to decode the URL before reading 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.
Three methods 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, such as the existing The database encoding uses GB2312. When interacting with JSON, 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.
So far, the three solutions to garbled Chinese characters in PHP JSON have been introduced. In fact, in PHP development, when encountering similar interactions between PHP and AJAX/JS, these methods are basically used when Chinese garbled characters are generated. The idea is similar.
Related recommendations: php training
The above is the detailed content of How to solve garbled Chinese characters in php json. For more information, please follow other related articles on the PHP Chinese website!