[Go]php return JSON data in Chinese display problem

Source: Internet
Author: User
Tags urlencode vars

Transferred from: http://blog.csdn.net/superbirds/article/details/8091910

Workaround:

[PHP]View Plaincopy
  1. <?php
  2. function Notice () {
  3. include './include/conn.php '; //Database link file
  4. $sql _notice = mysql_query (' SELECT * from gg_notice where enable = "1" limit 0,10 ');
  5. $notice = mysql_fetch_array ($sql _notice, MYSQL_ASSOC);
  6. $str = Json_encode ($notice);
  7. //linux
  8. return Preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $str);
  9. //windows
  10. //return preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "Iconv (' Ucs-2le ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $STR);
  11. }
  12. ?>

Other ways to search from the Internet

[PHP]View Plaincopy
  1. <?php
  2. /**
  3. * JSON generation, analysis support Chinese
  4. */
  5. Class Json_helper {
  6. /** 
  7. * Generate JSON
  8. */
  9. public static function encode ($str) {
  10. $json = Json_encode ($str);
  11. //linux
  12. return Preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $json);
  13. //windows
  14. //return preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "Iconv (' Ucs-2le ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $json);
  15. }
  16. /** 
  17. * Analysis JSON
  18. */
  19. public static function decode ($str) {
  20. return Json_decode ($str);
  21. }
  22. }
  23. ?>

This is another related article from the online search

When encoding data using PHP's own Json_encode, Chinese will become Unicode, resulting in unreadable. For example: After Json_encode the string "Xiamen", the output is "\u53a6\u95e8".

There are two ways to look at the query:
1. Restore "\u53a6\u95e8" to "Xiamen" using the following code:

[PHP]View Plaincopy
    1. $str = Preg_replace ("#\\\u ([0-9a-f]+) #ie", "iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $str);

2. After urlencode,json_encode the Chinese text paragraph, then use the UrlDecode, you can also display the English language.

[PHP]View Plaincopy
    1. $code = UrlDecode (Json_encode (UrlEncode ("Xiamen"));

PHP5.4 version, a new option has been added to JSON: Json_unescaped_unicode. With this option, the Chinese is not automatically encoded. [PHP]View Plaincopy
    1. Echo Json_encode ("Xiamen", Json_unescaped_unicode);

Also, since Json_encode and Json_decode only support utf-8 encoded characters, the GBK characters have to be converted with JSON, with their own GBK to UTF-8 code:

[PHP]View Plaincopy
  1. /*
  2. The string GBK transcoded to UTF-8, and the numbers are converted to numbers.
  3. */
  4. function Ct2 ($s) {
  5. if (is_numeric ($s)) {
  6. return intval ($s);
  7. } Else {
  8. return Iconv ("GBK","UTF-8",$s);
  9. }
  10. }
  11. /*
  12. Batch processing Gbk->utf-8
  13. */
  14. function Icon_to_utf8 ($s) {
  15. if (is_array ($s)) {
  16. foreach ($s as $key = = $val) {
  17. $s [$key] = Icon_to_utf8 ($val);
  18. }
  19. } Else {
  20. $s = ct2 ($s);
  21. }
  22. return $s;
  23. }
  24. Echo Json_encode (Icon_to_utf8 ("Xiamen"));

[Go]php return JSON data in Chinese display problem

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.