php unicode编码转成unioce字符(中文)

有几个unicode编码如下:
\u4e2d\u56fd\u4eba

如果将其换成字符的形式呢?

<html>
<head>
    <meta charset="utf-8">
</head>
<body>
	
<?php
header("Content-type: text/html; charset=utf-8");

echo date('H:i:s <br/>', time());

echo '\u4e2d ';
echo iconv('UCS-2', 'UTF-8', pack('H4', '4e2d'));
echo '<br/>';

$unicodeStr = '\u4e2d\u56fd\u4eba';
echo "$unicodeStr <br/>";

// 方法1,PHP5.5及之前的版本,preg_replacez在PHP5.5后过时了?
$str = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $unicodeStr);
echo "方法1:$str <br/>";


// 方法2,PHP5.6及更高版本使用
function match_string($matches)
{
    return  iconv('UCS-2', 'UTF-8', pack('H4', $matches[1]));
    //return  iconv('UCS-2BE', 'UTF-8', pack('H4', $matches[1]));
    //return  iconv('UCS-2LE', 'UTF-8', pack('H4', $matches[1]));
}

$str = preg_replace_callback("#\\\u([0-9a-f]{4})#", 'match_string', $unicodeStr);
//$str = preg_replace_callback("/\\\u([0-9a-f]{4})/", 'match_string', $unicodeStr);
echo "方法2:$str <br/>";;



// 方法3,PHP5.6及更高版本使用
function replace_unicode_escape_sequence($match)
{       
		return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');     
}          
$str = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $unicodeStr);    
echo "方法3:$str <br/>";


?>




</body>
</html>
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

friendan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值