Solution to Chinese characters garbled by json_encode in php

Source: Internet
Author: User
The general solution is to ensure that the characters are UTF-8 encoded during JSON processing. For details, we can change the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can

The general solution is to ensure that the characters are UTF-8 encoded during JSON processing. For details, we can change the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the character into UTF8 format before JSON processing.

I. json_encode ()

This is a common function for JSON encoding of variables. However, when the text format is not UTF-8, Chinese transcoding may encounter some problems, such as when the text is gb2312, for example:

  1. $ JsonText = array (
  2. 0 => array (
  3. 'Id' => '1 ',
  4. 'Name' => 'text 1'
  5. ),
  6. 1 => array (
  7. 'Id' => '2 ',
  8. 'Name' => 'text 2'
  9. )
  10. );
  11. Echo json_encode ($ jsonText );
  12. // [{"Id": "1", "name": "" },{ "id": "2", "name": ""}]
  13. // We can see that the Chinese characters are not escaped as empty "", because json only escapes the encoding (similar to: % B0 % AE), so the preceding statement should first convert the encoding
  14. ?>

Solution:

  1. Foreach ($ jsonText as $ key => $ value)
  2. {
  3. $ JsonText [$ key] ['name'] = urlencode ($ value ['name']);
  4. }
  5. Echo json_encode ($ jsonText );
  6. ?>

Client processing:

  1. Function encodeTest (obj)
  2. {
  3. $. Ajax ({
  4. Type: "GET ",
  5. Url: "<? = $ This-> baseUrl?> /Index/getajax ",
  6. Data: "c =" obj. value,
  7. Success: function (json)
  8. {
  9. Var json = eval (json );
  10. Var testValue = '';
  11. $. Each (json, function (k ){
  12. TestValue + = decodeURI (json [k] ['name']);
  13. });
  14.  
  15. Alert (testValue );
  16. }
  17. })
  18.  
  19. }
  20. Script

The above code js reports that the encoding does not conform to the standard because decodeURI in js only supports utf8 transcoding. Therefore, the code of the PHP json_encode function should be the following code:

  1. Foreach ($ jsonText as $ key => $ value)
  2. {
  3. $ JsonText [$ key] ['name'] = urlencode (iconv ('gb2312', 'utf-8', $ value ['name']);
  4. }
  5. Echo json_encode ($ json );
  6. ?>

The output result is {"name": "u4e2du6587u5b57u7b26u4e32", "value": "test"}. it can be seen that UTF-8 characters are used, and json_encode also contains Chinese characters. The solution is to use the urlencode () function to process the characters before using json_encode, and then use json_encode to return the urldecode () function when outputting the results. The details are as follows:

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

Check that the output result is {"name": "Chinese string", "value": "test"}. at this point, the Chinese character is successfully output. use json_encode as needed, in this way, the JSON string output in the PHP background will not contain Chinese garbled characters after Ajax is received in the front-end javascript, because js is also processing JSON format data in the form of UTF8, similar to PHP, the JSON string that receives the PHP page will not be faulty.

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.