php5.4 before the Json_encode when the Chinese will be Unicode encoding, Chinese will be encoded, become unreadable, similar to the "\u***" format, but also to a certain extent, increase the amount of data transferred.
For example:
Copy CodeThe code is as follows:
$str = ' Chinese ';
echo Json_encode ($STR);
"\u4e2d\u6587"
php5.4 start
Echo Json_encode ($str, Json_unescaped_unicode);
Chinese
php5.4 let JSON know more Chinese!
no Unicode transcoding before 5.4, there are 3 ways to handle:
But in fact the first two ways are to cause problems in some special cases. The following 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 Weekly water sub-airport ' → Renmin Road ',
1 = ' Operating Hours: 5:10~21:00 fare: 16 yuan interval of 20 minutes, full at any time of the departure ',
);
/*
Encode and decode by regular Json_encode|json_decode
["\" \u5927\u8fde\u5468\u6c34\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 Weekly water sub-airport ' → Renmin Road ',
1 = ' Operating Hours: 5:10~21:00 fare: 16 yuan interval of 20 minutes, full at any time of the departure ',
)
*/
$test 1 = json_encode ($test);
$test 2 = Json_decode ($test 1, TRUE);
echo $test 1;
Echo Php_eol;
Var_export ($test 2);
Echo Php_eol;
/*
Myjson|json_decode to encode and decode, but will give an error json_last_error return (Json_error_syntax = = = 4),
Because "Dalian weekly water sub-airport" → Renmin Road "
["" "Dalian weekly water sub-airport" → Renmin Road "," Operating Hours: 5:10~21:00 fare: 16 yuan departure interval 20 minutes, full time departure "]
Null
*/
$test 1_1 = Myjson ($test);
$test 2_1 = Json_decode ($test 1_1, TRUE);
echo $test 1_1;
Echo Php_eol;
Var_export ($test 2_1);
Echo Php_eol;
/*
Encode and decode with Json_enco+pack|json_decode to ensure no Chinese encoding, but lack of operating time data and fares
["\" Dalian weekly water sub-airport \ "→ Renmin Road", "Operating Hours:: ~: Fares: The yuan departs every minute, full time departure"]
Array (
0 = ' Dalian Weekly water sub-airport ' → Renmin Road ',
1 = ' Operating Hours:: ~: Fares: The yuan departs every minute, the bus departs at any time ',
)
*/
function Replaceuni ($STR) {
Return Preg_replace ("#\\\u ([0-9a-f]+) #ie", "Iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $STR);
}
$test 1_2 = Replaceuni (Json_encode ($test));
$test 2_2 = Json_decode ($test 1_2, TRUE);
echo $test 1_2;
Echo Php_eol;
Var_export ($test 2_2);
Echo Php_eol;
Finally summarize a sentence, recommend upgrade to PHP5.4, let PHP more understand Chinese!
http://www.bkjia.com/PHPjc/326510.html www.bkjia.com true http://www.bkjia.com/PHPjc/326510.html techarticle php5.4 before the Json_encode when the Chinese will be Unicode encoding, Chinese will be encoded, become unreadable, similar to the "\u***" format, but also to a certain extent, to increase the transmission of data ...