This article mainly introduces how to solve the problem of Chinese Character Display in PHPJSON format. This article summarizes three methods to solve the problem of Chinese character display starting with u. For more information, see
This article mainly introduces how to solve the problem of Chinese display in php json format. This article summarizes three methods to solve the problem of Chinese display starting with \ u. For more information, see
Returns a question about Chinese display of json data.
In the previous article, the returned Chinese Characters in json format are displayed as \ u5723 \ u8bde \ u8282 \ u5343 \ u4e07 \ u597d \ u793c \ u5927 \ u5949 \ u9001
Solution 1:
The Code is 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 methods searched from the Internet
The Code is as follows:
<? Php
/**
* Json generation and analysis support Chinese Characters
*/
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 );
}
/**
* Json Analysis
*/
Public static function decode ($ str ){
Return json_decode ($ str );
}
}
?>
This is another related article from the Internet.
When you use the json_encode that comes with php to encode the data, the Chinese character will become unicode, leading to unreadable. For example, after json_encode is performed on the string "Xiamen", the output is "\ u53a6 \ u95e8 ".
The following two methods are available:
1. Restore "\ u53a6 \ u95e8" to "Xiamen" and use the following code:
The Code is as follows:
$ Str = preg_replace ("# \\\ u ([0-9a-f] +) # ie", "iconv ('ucs-2', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ str );
2. urlencode, json_encode, and urldecode are used to display Chinese characters.
The Code is as follows:
$ Code = urldecode (json_encode (urlencode ("Xiamen ")));
PHP5.4 has added the option JSON_UNESCAPED_UNICODE to Json. After this option is added, Chinese characters are not automatically encoded.
The Code is as follows:
Echo json_encode ("Xiamen", JSON_UNESCAPED_UNICODE );
In addition, because json_encode and json_decode only support UTF-8 characters, GBK characters must be converted to json, with their own write GBK to UTF-8 code:
The Code is as follows:
/*
The string GBK is transcoded to a 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 "));
,