This article introduces how to solve Chinese garbled characters when operating JSON in php programming. if you need it, please refer to it. Chinese garbled characters are common when Ajax technology is used to interact with PHP backend. JSON is used as a data exchange format similar to XML, chinese characters may also be garbled during PHP interaction. The solution for php json Chinese garbled characters is similar to the solution for PHP Ajax to transmit Chinese characters with garbled characters. The following describes several methods. Reasons for Chinese garbled characters during PHP and JSON interaction: because JSON and JS are the same, the client-side characters are processed in the form of UTF8, that is, the characters returned for JSON submission or receiving 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. I. Method 1 to solve PHP JSON Chinese garbled characters: ensure the consistency of PHP page codes in the database and the front and back ends, and ensure that the database encoding and PHP page encoding are in the UTF8 format, for beginners of PHP, UTF8 format is recommended for database creation, which can reduce a lot of trouble. Method 2: configure to use the PHP urlencode function and the JS function decodeURI () When PHP uses JSON to interact with foreground JS, use the urlencode function for URL encoding before using the json_encode function, the JS client uses the decodeURI () function to perform URL decoding and then 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: use the string encoding conversion function when the database encoding and PHP page encoding are inconsistent for various reasons, 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. 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. These are the three solutions to php json Chinese character garbled characters. I hope this will help you. In php development, when PHP interacts with AJAX/JS and generates Chinese garbled characters, most of them use these methods to easily solve the problem. |