Transferred from: http://blog.csdn.net/superbirds/article/details/8091910
Workaround:
[PHP]View Plaincopy
- <?php
- function Notice () {
- include './include/conn.php '; //Database link file
- $sql _notice = mysql_query (' SELECT * from gg_notice where enable = "1" limit 0,10 ');
- $notice = mysql_fetch_array ($sql _notice, MYSQL_ASSOC);
- $str = Json_encode ($notice);
- //linux
- return Preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $str);
- //windows
- //return preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "Iconv (' Ucs-2le ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $STR);
- }
- ?>
Other ways to search from the Internet
[PHP]View Plaincopy
- <?php
- /**
- * JSON generation, analysis support Chinese
- */
- Class Json_helper {
- /**
- * Generate JSON
- */
- public static function encode ($str) {
- $json = Json_encode ($str);
- //linux
- return Preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $json);
- //windows
- //return preg_replace ("#\\\u ([0-9a-f]{4}) #ie", "Iconv (' Ucs-2le ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $json);
- }
- /**
- * Analysis JSON
- */
- public static function decode ($str) {
- return Json_decode ($str);
- }
- }
- ?>
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
- $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
- $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
- 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
- /*
- The string GBK transcoded to UTF-8, and the numbers are converted to numbers.
- */
- function Ct2 ($s) {
- if (is_numeric ($s)) {
- return intval ($s);
- } Else {
- return Iconv ("GBK","UTF-8",$s);
- }
- }
- /*
- Batch processing Gbk->utf-8
- */
- function Icon_to_utf8 ($s) {
- if (is_array ($s)) {
- foreach ($s as $key = = $val) {
- $s [$key] = Icon_to_utf8 ($val);
- }
- } Else {
- $s = ct2 ($s);
- }
- return $s;
- }
- Echo Json_encode (Icon_to_utf8 ("Xiamen"));
[Go]php return JSON data in Chinese display problem