Change in json_encode Chinese transcoding in PHP5.4 [make json better understand Chinese]
Last Update:2014-05-15
Source: Internet
Author: User
In json_encode before php5.4, Chinese characters are unicode encoded and Chinese characters are encoded and become unreadable, similar to \ u *** format, it also increases the amount of data transmitted to a certain extent. Example: & lt ;? Php $ str = & amp; #39; Chinese & amp; #39; echojson_encode ($ str... "/> 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: $ 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 at intervals of 20 minutes and departs at any time ',); /* codec ["\" \ u5927 \ u8fde \ u5468 \ u6c34 \ u5b50 \ u673a \ u573a \ "\ u2192 \ u4eba \ u6c11 \ u8def", "\ Users \ u8425 \ u65f6 \ u95f4 \ uff1a5 \ Users \ Alibaba \ u7968 \ Users \ u5143 \ u53d1 \ u8f66 \ u95f4 \ Users \ 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;/* return json_last_error (JSON_ERROR_SYNTAX = 4) through myjson | json_decode ), because "Dalian Zhoushuizi Airport" → Renmin Road "[" "Dalian zhoushuzi 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, however, there will be a lack of operation time data and fares ["\" Dalian Zhoushuizi Airport \ "→ Renmin Road", "Operation Time ::~: Fare: Yuan departs every minute and departs at any time. "] array (www.2cto.com 0 => '" Dalian Zhoushuizi Airport "→ Renmin Road', 1 => 'operation Time ::~: Fare: Yuan departs every minute and departs at any time. ',) */function replaceUni ($ str) {return preg_replace ("# \\\ u ([0-9a-f] +) # ie "," iconv ('ucs-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;