In json_encode before php5.4, Chinese characters are unicode encoded and Chinese characters are encoded and become unreadable, similar to the format of "\ u, it also increases the amount of data transmitted to a certain extent.
For example:
Copy codeThe Code is as follows:
<? Php
$ Str = 'Chinese ';
Echo json_encode ($ str );
// "\ U4e2d \ u6587"
Php5.4
Echo json_encode ($ str, JSON_UNESCAPED_UNICODE );
// "Chinese"
Php5.4 make json better understand Chinese!
Unicode transcoding is not performed before 5.4. There are three ways to handle this problem.:
However, the first two methods may cause problems in some special cases. For example:
Copy codeThe Code is as follows:
Function myjson ($ code ){
$ Code = json_encode (urlencodeAry ($ code ));
Return urldecode ($ code );
}
Function urlencodeAry ($ data ){
If (is_array ($ data )){
Foreach ($ data as $ key => $ val ){
$ Data [$ key] = urlencodeAry ($ val );
}
Return $ data;
} Else {
Return urlencode ($ data );
}
}
$ Test = array (
0 => '"Dalian zhoushuizi airport" → Renmin Road ',
1 => 'Operation time: 5: 10 ~ Fare: $16 departs every 20 minutes and departs at any time ',
);
/*
Codec through the standard json_encode | json_decode
["\" \ U5927 \ u8fde \ u5468 \ Alibaba \ u5b50 \ u673a \ u573a \ "\ u2192 \ u4eba \ u6c11 \ u8def", "\ u8fd0 \ u8425 \ u65f6
\ U95f4 \ uff1a5 \ uff1a10 \ uff5e21 \ uff1a00 \ u7968 \ u4ef7 \ uff1a16 \ u5143 \ u53d1 \ u8f66 \ u95f4 \ u969420
\ U5206 \ u949f \ u4e00 \ u73ed \ uff0c \ u5ba2 \ u6ee1 \ u968f \ u65f6 \ u53d1 \ u8f66 "]
Array (
0 => '"Dalian zhoushuizi airport" → Renmin Road ',
1 => 'Operation time: 5: 10 ~ Fare: $16 departs every 20 minutes and departs at any time ',
)
*/
$ Test1 = json_encode ($ test );
$ Test2 = json_decode ($ test1, TRUE );
Echo $ test1;
Echo PHP_EOL;
Var_export ($ test2 );
Echo PHP_EOL;
/*
Codec through myjson | json_decode, but an error json_last_error is returned (JSON_ERROR_SYNTAX = 4 ),
Because "Dalian zhoushuizi airport" → Renmin Road"
["Dalian zhoushuizi airport" → Renmin Road "," Operation Time: 5: 10 ~ Fare: $16 departs every 20 minutes and departs at any time. "]
NULL
*/
$ Test1_1 = myjson ($ test );
$ Test2_1 = json_decode ($ test1_1, TRUE );
Echo $ test1_1;
Echo PHP_EOL;
Var_export ($ test2_1 );
Echo PHP_EOL;
/*
Codec through json_enco + pack | json_decode to ensure that Chinese characters are not encoded, but operation time data and fares are missing
["\" Dalian zhoushuizi airport \ "→ Renmin Road", "Operation Time ::~ : Fare: Yuan departs at intervals of minutes and departs at any time. "]
Array (
0 => '"Dalian zhoushuizi airport" → Renmin Road ',
1 => 'Operation time ::~ : Fare: Yuan departs at intervals of minutes and departs at any time ',
)
*/
Function replaceUni ($ str ){
Return preg_replace ("# \\\ u ([0-9a-f] +) # ie", "iconv ('ucos-2', 'utf-8', pack ('h4 ', '\ 1') ", $ str );
}
$ Test1_2 = replaceUni (json_encode ($ test ));
$ Test2_2 = json_decode ($ test1_2, TRUE );
Echo $ test1_2;
Echo PHP_EOL;
Var_export ($ test2_2 );
Echo PHP_EOL;
In conclusion, we recommend that you upgrade PHP to PHP5.4 to better understand Chinese!