Summary of json_encode Chinese transcoding in PHP5.4
- 2020-05-27 04:37:23
- OfStack
When json_encode is done before php5.4, Chinese will be encoded by unicode, and Chinese will be encoded and become unreadable, similar to the format of "\u***", and the amount of data transmitted will be increased to a certain extent.
Such as:
php5.4 let json know more Chinese!
5.4 unicode transcoding is not carried out before, and there are three ways to deal with it:
But the first two ways are actually going to cause problems, in some special cases. The following cases:
In the last sentence, it is recommended to upgrade to PHP 5.4, so that PHP can understand more Chinese!
Such as:
<?php
$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!
5.4 unicode transcoding is not carried out before, and there are three ways to deal with it:
But the first two ways are actually going to cause problems, in some special cases. The following cases:
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 ~ 21 : 00 Price: 16 yuan Departure interval 20 minutes 1 Ben, we leave whenever we are full ',
);
/*
By routine json_encode|json_decode To make up the decoding
["\"\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 zhoushuizi airport " Renmin road, ',
1 => ' Operation time: 5 : 10 ~ 21 : 00 Price: 16 yuan Departure interval 20 minutes 1 Ben, we leave whenever we are full ',
)
*/
$test1 = json_encode($test);
$test2 = json_decode($test1, TRUE);
echo $test1;
echo PHP_EOL;
var_export($test2);
echo PHP_EOL;
/*
through myjson|json_decode To encode and decode, but there will be an error json_last_error return (JSON_ERROR_SYNTAX === 4) .
because "" Dalian zhoushuizi airport " Renmin road, "
["" Dalian zhoushuizi airport " Renmin road, "," Operation time: 5 : 10 ~ 21 : 00 Price: 16 yuan Departure interval 20 minutes 1 Ben, we leave whenever we are full "]
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;
/*
through json_enco+pack|json_decode To encode and decode, to ensure that it does not encode Chinese, but will be short of operating time data and ticket prices
["\" Dalian zhoushuizi airport \" Renmin road, "," Operation time: : ~ : RMB: Minutes apart 1 Ben, we leave whenever we are full "]
array (
0 => '" Dalian zhoushuizi airport " Renmin road, ',
1 => ' Operation time: : ~ : RMB: Minutes apart 1 Ben, we leave whenever we are full ',
)
*/
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;
In the last sentence, it is recommended to upgrade to PHP 5.4, so that PHP can understand more Chinese!