有几个unicode编码如下:
\u4e2d\u56fd\u4eba
\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>