Returns the question of the Chinese display of JSON data
In the previous article, the Chinese returned in JSON format as \u5723\u8bde\u8282\u5343\u4e07\u597d\u793c\u5927\u5949\u9001
Workaround One:
Copy Code code as follows:
<?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
Copy Code code as follows:
<?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);
}
/**
* Parsing JSON
*/
public static function decode ($STR) {
Return Json_decode ($STR);
}
}
?>
This is another related article from the Internet search.
When the data is encoded using PHP's Json_encode, the Chinese language becomes Unicode, causing the unreadable. For example: After the string "Xiamen" carries on the Json_encode, the output is "\u53a6\u95e8".
Query, there are two ways:
1. Restore "\u53a6\u95e8" to "Xiamen" using the following code:
Copy Code code as follows:
$str = Preg_replace ("#\\\u" ([0-9a-f]+) #ie "," Iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 ')) ", $STR);
2. The Chinese text section is Urlencode,json_encode, and then the UrlDecode can also be displayed.
Copy Code code as follows:
$code = UrlDecode (Json_encode (UrlEncode ("Xiamen"));
PHP5.4 version, a new option has been added to JSON: Json_unescaped_unicode. Adding this option will not automatically encode the Chinese.
Copy Code code as follows:
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:
Copy Code code as follows:
/*
The string GBK is UTF-8 and the number is converted to a number.
*/
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"));