Discussion on the solution of PHP json Chinese garbled _php tips

Source: Internet
Author: User
Tags php json urlencode

We know that in the use of AJAX technology and PHP background interaction, the Chinese garbled is often the case, JSON as a similar to XML data Interchange Format, in PHP for interaction will also appear in Chinese garbled situation, to solve the PHP JSON Chinese garbled method of thinking in fact, and PHP Ajax to pass the value of character garbled solution similar to the following I take the form of a tutorial in detail to solve the PHP JSON Chinese garbled method.

Why does PHP have Chinese garbled characters when interacting with JSON?

Because JSON is the same as JS, the characters for the client are processed in UTF8 form, that is, JSON submits or accepts the returned characters as UTF8, and when interacting with PHP, if the database Code, PHP page encoding is not in UTF8 format, When PHP interacts with JSON, it will produce garbled characters.

Ways to solve PHP json Chinese garbled

In the knowledge of the PHP and JSON interaction between the reasons for the Chinese garbled solution is actually a lot simpler.

To solve the PHP JSON Chinese garbled method one: To ensure that the database, front and back PHP page code consistent

The simplest way to solve the PHP JSON Chinese garbled method is to ensure that the database code, PHP page encoding unified using UTF8 format, for the PHP beginners to build a station when the database is best to use the UTF8 format, can reduce a lot of trouble.

Solve PHP JSON Chinese garbled method Two: Configure to use PHP function UrlEncode and JS function decodeURI ()

When PHP uses JSON to interact with the foreground JS, you need to use the UrlEncode function before using the Json_encode function to encode the URL, and then read the Chinese message in the JS client via the JS function decodeURI () after the URL is decoded.

Note: When interacting via JSON in a PHP page, such as converting an array to JSON format, first use the UrlEncode function to encode the key and value of the URL and then use the Json_encode function and the UrlDecode function.

Solving PHP JSON Chinese garbled method three: Using string encoding conversion function

When the database code, PHP page encoding is inconsistent, for various reasons, such as the existing database encoding using GB2312, when using JSON interaction, you need to use the string encoding conversion function to convert between character sets. Commonly used functions have iconv, because iconv need PHP configuration environment support, if not supported, you can write GBK and UTF8 encoded conversion functions to achieve the encoding conversion.

At this point, PHP JSON characters garbled three kinds of solutions are introduced, in fact, in PHP development, encountered similar to PHP and Ajax/js interaction, the production of Chinese garbled when the basic use of these methods, 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.