php json_encode 后, 汉字被转移了
$arr=array('这是什么编码');echojson_encode($arr);输出的结果:["\u8fd9\u662f\u4ec0\u4e48\u7f16\u78...
$arr = array('这是什么编码');
echo json_encode($arr);
输出的结果: ["\u8fd9\u662f\u4ec0\u4e48\u7f16\u7801"]
哪位大神指点下 , 这个被转义后的汉字是什么编码呢 展开
echo json_encode($arr);
输出的结果: ["\u8fd9\u662f\u4ec0\u4e48\u7f16\u7801"]
哪位大神指点下 , 这个被转义后的汉字是什么编码呢 展开
1个回答
展开全部
属于unicode编码
转换自己做个函数就好!
<?php
/**
* 将 %u4F19 转换成如 伙 的 HTML Entity 的形式
*
* @param mixed $str
* @access public
* @return void
*/
function convert_entities($str) {
$str = preg_replace_callback('|%u([a-f0-9]{4})|i',
create_function(
'$matches',
'return \'\' . hexdec($matches[1]) . \';\';'
),
$str
);
return $str;
}
/**
* 把 HTML Entity 转换为原始字符
*
* @param mixed $source
* @access public
* @return void
*/
function utf8encode($source) {
$utf8str = '';
$entities = explode('', $source);
$size = count($entities);
for ($i = 0; $i < $size; $i++) {
$foo = $entities[$i];
$nonEntity = strstr($foo, ';');
if ($nonEntity !== false) {
$unicode = intval(substr($foo, 0, (strpos($foo, ';') + 1)));
// determine how many chars are needed to reprsent this unicode char
if ($unicode < 128) {
$bar = chr($unicode);
}
else if ($unicode >= 128 and $unicode < 2048) {
$binVal = str_pad(decbin($unicode), 11, '0', STR_PAD_LEFT);
$binPart1 = substr($binVal, 0, 5);
$binPart2 = substr($binVal, 5);
$char1 = chr(192 + bindec($binPart1));
$char2 = chr(128 + bindec($binPart2));
$bar = $char1 . $char2;
}
else if ($unicode >= 2048 and $unicode < 65536) {
$binVal = str_pad(decbin ($unicode), 16, '0', STR_PAD_LEFT);
$binPart1 = substr($binVal, 0, 4);
$binPart2 = substr($binVal, 4, 6);
$binPart3 = substr($binVal, 10);
$char1 = chr(224 + bindec($binPart1));
$char2 = chr(128 + bindec($binPart2));
$char3 = chr(128 + bindec($binPart3));
$bar = $char1 . $char2 . $char3;
}
else {
$binVal = str_pad(decbin($unicode), 21, '0', STR_PAD_LEFT);
$binPart1 = substr($binVal, 0, 3);
$binPart2 = substr($binVal, 3, 6);
$binPart3 = substr($binVal, 9, 6);
$binPart4 = substr($binVal, 15);
$char1 = chr(240 + bindec($binPart1));
$char2 = chr(128 + bindec($binPart2));
$char3 = chr(128 + bindec($binPart3));
$char4 = chr(128 + bindec($binPart4));
$bar = $char1 . $char2 . $char3 . $char4;
}
if (strlen($nonEntity) > 1) {
$nonEntity = substr($nonEntity, 1); // chop the first char (';')
}
else {
$nonEntity = '';
}
$utf8str .= $bar . $nonEntity;
}
else {
$utf8str .= $foo;
}
}
return $utf8str;
}
$str = '\u662f\u56fd\u5185\u6bbf\u5802\u7ea7\u5316\u5986\u5e08,\u8fd9\u6b21...';
$str=str_replace('\\','%',$str);
$str = convert_entities($str);
header('Content-Type:text/html;charset=utf-8');
print utf8encode($str);
转换自己做个函数就好!
<?php
/**
* 将 %u4F19 转换成如 伙 的 HTML Entity 的形式
*
* @param mixed $str
* @access public
* @return void
*/
function convert_entities($str) {
$str = preg_replace_callback('|%u([a-f0-9]{4})|i',
create_function(
'$matches',
'return \'\' . hexdec($matches[1]) . \';\';'
),
$str
);
return $str;
}
/**
* 把 HTML Entity 转换为原始字符
*
* @param mixed $source
* @access public
* @return void
*/
function utf8encode($source) {
$utf8str = '';
$entities = explode('', $source);
$size = count($entities);
for ($i = 0; $i < $size; $i++) {
$foo = $entities[$i];
$nonEntity = strstr($foo, ';');
if ($nonEntity !== false) {
$unicode = intval(substr($foo, 0, (strpos($foo, ';') + 1)));
// determine how many chars are needed to reprsent this unicode char
if ($unicode < 128) {
$bar = chr($unicode);
}
else if ($unicode >= 128 and $unicode < 2048) {
$binVal = str_pad(decbin($unicode), 11, '0', STR_PAD_LEFT);
$binPart1 = substr($binVal, 0, 5);
$binPart2 = substr($binVal, 5);
$char1 = chr(192 + bindec($binPart1));
$char2 = chr(128 + bindec($binPart2));
$bar = $char1 . $char2;
}
else if ($unicode >= 2048 and $unicode < 65536) {
$binVal = str_pad(decbin ($unicode), 16, '0', STR_PAD_LEFT);
$binPart1 = substr($binVal, 0, 4);
$binPart2 = substr($binVal, 4, 6);
$binPart3 = substr($binVal, 10);
$char1 = chr(224 + bindec($binPart1));
$char2 = chr(128 + bindec($binPart2));
$char3 = chr(128 + bindec($binPart3));
$bar = $char1 . $char2 . $char3;
}
else {
$binVal = str_pad(decbin($unicode), 21, '0', STR_PAD_LEFT);
$binPart1 = substr($binVal, 0, 3);
$binPart2 = substr($binVal, 3, 6);
$binPart3 = substr($binVal, 9, 6);
$binPart4 = substr($binVal, 15);
$char1 = chr(240 + bindec($binPart1));
$char2 = chr(128 + bindec($binPart2));
$char3 = chr(128 + bindec($binPart3));
$char4 = chr(128 + bindec($binPart4));
$bar = $char1 . $char2 . $char3 . $char4;
}
if (strlen($nonEntity) > 1) {
$nonEntity = substr($nonEntity, 1); // chop the first char (';')
}
else {
$nonEntity = '';
}
$utf8str .= $bar . $nonEntity;
}
else {
$utf8str .= $foo;
}
}
return $utf8str;
}
$str = '\u662f\u56fd\u5185\u6bbf\u5802\u7ea7\u5316\u5986\u5e08,\u8fd9\u6b21...';
$str=str_replace('\\','%',$str);
$str = convert_entities($str);
header('Content-Type:text/html;charset=utf-8');
print utf8encode($str);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询