Solutions to garbled JSON files opened by php: 1. Ensure that the encoding of the database, front and back PHP pages is consistent; 2. Configure and use the PHP function urlencode and JS function decodeURI(); 3. Use the string encoding conversion function.
The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer
What should I do if php opens a json file with garbled characters?
Comprehensive solution to Chinese garbled characters in PHP JSON
We know that Chinese garbled characters are common when using Ajax technology to interact with the PHP background. JSON as A data exchange format similar to XML, Chinese garbled characters will also appear when PHP is used for interaction. The method to solve the Chinese garbled characters in PHP JSON is actually similar to the solution to the Chinese characters garbled in PHP Ajax value transfer. Below I will use a tutorial format. Let’s introduce in detail the method to solve the Chinese garbled code in PHP JSON.
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 returned by JSON submission or acceptance are processed in the form of UTF8. When interacting with PHP, if the database encoding and PHP page encoding When UTF8 format is not used, Chinese characters will be garbled when PHP interacts with JSON.
Methods to solve Chinese garbled characters in PHP JSON
After knowing the reasons why Chinese garbled characters are generated when PHP interacts with JSON, 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 back 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.
Method 2 to solve the Chinese garbled code in PHP JSON:
Configure and use the PHP function urlencode and the 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.
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, 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.
At this point, 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.
Recommended: "PHP Video Tutorial"
The above is the detailed content of What should I do if php opens a json file with garbled characters?. For more information, please follow other related articles on the PHP Chinese website!