Change in json_encode Chinese transcoding in PHP5.4 [make json better understand Chinese]

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.