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

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

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



    


	
', time());

echo '\u4e2d ';
echo iconv('UCS-2', 'UTF-8', pack('H4', '4e2d'));
echo '
'; $unicodeStr = '\u4e2d\u56fd\u4eba'; echo "$unicodeStr
"; // 方法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
"; // 方法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
";; // 方法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
"; ?>

php unicode编码转成unioce字符(中文)_第1张图片















你可能感兴趣的:(php)