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"]

哪位大神指点下 , 这个被转义后的汉字是什么编码呢
展开
 我来答
szKandy
2013-09-02
知道答主
回答量:8
采纳率:0%
帮助的人:6.6万
展开全部
属于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);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式