Phpjson_encodeutf-8 Chinese problems

Source: Internet
Author: User
In the past, the most common problem was that the json_encode was garbled during gbk encoding. Today, we found that uft8 also had Chinese garbled characters. let's take a look at how to solve the problem. UTF-8 json_encode

In the past, the most common problem was that the json_encode was garbled during gbk encoding. Today, we found that uft8 also had Chinese garbled characters. let's take a look at how to solve the problem.

The UTF-8 character json_encode is converted into utf16 encoding. the code is as follows:

  1. $./Php/bin/php-r 'echo json_encode ("Chinese ");'
  2. "U4e2du6587"

Reduced readability, the latest php 5.4 json_encode supports the UTF-8 encoding, can put the Chinese do not encode the direct output, then the lower version how to do? There are also ways to encapsulate a function and share it with you. the code is as follows:

  1. Function my_json_encode ($ var ){
  2. Return preg_replace ("/u ([a-f0-9] {4})/e", "iconv ('ucs-4le', 'utf-8', pack ('v ', hexdec ('U $ 1') ", json_encode ($ var ));
  3. }

For example, using another solution, the background PHP page (page code as UTF-8 or has converted the character into a UTF-8) using json_encode to convert the array in PHP into a JSON string, for example:

  1. $ TestJSON = array ('name' => 'Chinese string', 'value' => 'test ');
  2. Echo json_encode ($ testJSON );
  3. ?>

The output result is as follows:

{"Name": "u4e2du6587u5b57u7b26u4e32", "value": "test "}

It can be seen that UTF-8 characters are used, and Chinese garbled characters are also used in json_encode. The solution is to use the urlencode () function to process the characters before using json_encode, and then use json_encode to return the output result using the urldecode () function. The details are as follows:

  1. $ TestJSON = array ('name' => 'Chinese string', 'value' => 'Www .phpfensi.com '');
  2. // Echo json_encode ($ testJSON );
  3. Foreach ($ testJSON as $ key => $ value ){
  4. $ TestJSON [$ key] = urlencode ($ value );
  5. }
  6. Echo urldecode (json_encode ($ testJSON ));
  7. ?>

The output result is as follows:

{"Name": "Chinese string", "value": "www.111cn.net "}

At this point, Chinese characters are successfully output.

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.