How to solve the problem of PHP JSON Chinese garbled _php tutorial

Source: Internet
Author: User
Tags php json
We know that in the use of AJAX technology and PHP background interaction, the Chinese garbled is often the case, JSON as an XML-like data Interchange format, in PHP used to interact with the Chinese language garbled, the way to solve the PHP JSON Chinese garbled method of thinking in fact with PHP Ajax value in the text characters are similar to the solution, the following I in detail in the tutorial format to solve the PHP JSON Chinese garbled method.

Why does PHP have garbled characters when interacting with JSON?

As JSON and JS, the character of the client is processed in the form of UTF8, that is, JSON commits or accepts the returned characters are processed in UTF8 form, when interacting with PHP, if the database encoding, PHP page encoding is not in the UTF8 format, When PHP interacts with JSON, Chinese will be garbled.

To solve the PHP JSON Chinese garbled method

It's a lot easier to figure out why PHP interacts with JSON to produce garbled characters.

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

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

Solve PHP JSON Chinese garbled method two: Configuration using PHP function UrlEncode and JS function decodeURI ()

When PHP uses JSON with the foreground JS interaction, before using the Json_encode function to use the UrlEncode function URL encoding, the JS client through the JS function decodeuri () URL decoding and then read the Chinese information.

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

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

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

At this point, PHP JSON characters garbled three ways to solve the introduction, in fact, in PHP development, encountered similar PHP and AJAX/JS to interact with the Chinese garbled when the basic use of these methods, the idea is similar.

http://www.bkjia.com/PHPjc/327407.html www.bkjia.com true http://www.bkjia.com/PHPjc/327407.html techarticle we know that in the use of AJAX technology and PHP background interaction, Chinese garbled is often the case, JSON as an XML-like Data Interchange Format, PHP used to interact with the Chinese garbled ...

  • Related Article

    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.