How to solve php JSON garbled characters _ php skills

Source: Internet
Author: User
Tags php json
This article provides a detailed analysis of the JSON Chinese garbled text solution in PHP. For more information, see what we know when Ajax technology is used to interact with the PHP background, chinese garbled characters are common. JSON, as a data exchange format similar to XML, may also cause text garbled characters when PHP is used for 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, urlencode must be used for URL encoding before using the json_encode function. in the JS client, decodeURI () is used through the JS function () after URL decoding, 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 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. 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.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.