Home >Backend Development >PHP Problem >How to deal with garbled Chinese characters in PHP JSON
Methods to solve PHP JSON Chinese garbled characters: 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 d [ecodeURI()]; 3. Use strings Encoding conversion function.
Methods to solve the Chinese garbled code in PHP JSON:
After knowing the reason why the interaction between PHP and JSON produces Chinese garbled code The final 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 adopts 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 # Before the ##json_encode function, you need to use the
urlencode function to perform URL encoding. On the JS client, use the JS function
decodeURI() to decode the URL and then read the Chinese information.
json_encode function and the
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 includeiconv. Since iconv requires support from the PHP configuration environment, if it is not supported, you can implement encoding conversion by writing your own gbk and UTF8 encoding conversion functions.
Relevant learning recommendations:
The above is the detailed content of How to deal with garbled Chinese characters in PHP JSON. For more information, please follow other related articles on the PHP Chinese website!