本文目录一览:
- 1、怎么让Json解析中文?
- 2、\u65b0\u5029\u5973\u5e7d\u9b42\这样的编码字符在php里该如何转换为原来的文字。
- 3、PHP UNICODE 编码转换
- 4、php 如何将unicode码转换成汉字
- 5、请问php如何把\u4e2d\u56fd 和\u534e\u5357 转换成中文
怎么让Json解析中文?
json里面的中文是Unicode编码后的,两个方案,php5.4版本以上的可以使用
echo json_encode("中文", JSON_UNESCAPED_UNICODE); //"中文"
代码输出中文
方案二,使用下面函数,对php版本没不限制
/**
* 不转义中文字符的 json 编码
* @param array $arr 待编码数组
* @return string
*/
function json($arr) {
$str = json_encode($arr);
$search = "#\\\u([0-9a-f]+)#ie";
$replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))";
return preg_replace($search, $replace, $str);
}
\u65b0\u5029\u5973\u5e7d\u9b42\这样的编码字符在php里该如何转换为原来的文字。
这是unicode编码吧,你用下面这个44
?php
/**
* 模拟JS里的unescape
*
* @param unknown_type $str
* @return unknown
*/
echo unescape('%u4E25');
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
$ar = $r[0];
#print_r($ar);
foreach($ar as $k=$v) {
if(substr($v,0,2) == "%u")
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
elseif(substr($v,0,3) == "")
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
elseif(substr($v,0,2) == "") {
echo substr($v,2,-1)."
";
$ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}
?
PHP UNICODE 编码转换
Unicode是一个字符集,Unicode是定长的都为双字节.
这里我们常用的是utf8字符集编码,楼主是说的Unicode转换为UTF-8吧。
/**
* Unicode字符转换成utf8字符
* @param [type] $unicode_str Unicode字符
* @return [type] Utf-8字符
*/
function unicode_to_utf8($unicode_str) {
$utf8_str = '';
$code = intval(hexdec($unicode_str));
//这里注意转换出来的code一定得是整形,这样才会正确的按位操作
$ord_1 = decbin(0xe0 | ($code 12));
$ord_2 = decbin(0x80 | (($code 6) 0x3f));
$ord_3 = decbin(0x80 | ($code 0x3f));
$utf8_str = chr(bindec($ord_1)) . chr(bindec($ord_2)) . chr(bindec($ord_3));
return $utf8_str;
}
php 如何将unicode码转换成汉字
$str = '$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"\u519c\u592b\u5c71\u6cc9","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}]-phppackunicode
$c=[{"_id":{"$id":"4fdaa7f3741d725816000000"},"bc":"012345678","name":"\u7ef4\u8fbe\u7eb8\u5dfe","cls":{"id":"125","name":"\u65e5\u7528\u54c1"},"std":{"name":"\u5f20\u6570","val":"10"}}]';-phppackunicode
$str=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);
echo $str;
乱码的话记得换下浏览器编码。....
请问php如何把\u4e2d\u56fd 和\u534e\u5357 转换成中文
这是unicode编码,
\u4e2d\u56fd 中国
\u534e\u5357 华南
------------------------------------
?php
$s = "\u4e2d\u56fd";
$s=str_replace("\\","%",$s);
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
$ar = $r[0];
#print_r($ar);
foreach($ar as $k=$v) {
if(substr($v,0,2) == "%u")
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
elseif(substr($v,0,3) == "")
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
elseif(substr($v,0,2) == "") {
echo substr($v,2,-1)."";
$ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}
echo unescape($s);
?