如何使用PHP5正确显示\uXXXX [英] How to get \uXXXX to display correctly, using PHP5

查看:150
本文介绍了如何使用PHP5正确显示\uXXXX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经继承了一个包含字符串的数据库,例如:

I have inherited a database which contains strings such as:

\\\卓\\\越\\\亚\\\马\\\逊:\\\网\ u4e0a\\\购\\\物:\\\在\\\线\\\销\\\售\\\图\\\书\\\,DVD\\\,CD\\\,\\\数\\\码\fe070c\\\玩\\\具 \\\,\\\家\\\居\\\,\\\化\\\妆

\u5353\u8d8a\u4e9a\u9a6c\u900a: \u7f51\u4e0a\u8d2d\u7269: \u5728\u7ebf\u9500\u552e\u56fe\u4e66\uff0cDVD\uff0cCD\uff0c\u6570\u7801\uff0c\u73a9\u5177\uff0c\u5bb6\u5c45\uff0c\u5316\u5986

问题是,如何让它在HTML页面中正确显示

The question is, how do I get this to be displayed properly in an HTML page?

我正在使用PHP5处理字符串。

I'm using PHP5 to process the strings.

推荐答案

根据daremon的提交,这里是一个unicode_decode函数,它将\uXXXX转换成UTF对应的。

Based on daremon's submission, here is a "unicode_decode" function which will convert \uXXXX into their UTF counterparts.

function unicode_decode($str){
    return preg_replace("/\\\u([0-9A-F]{4})/ie", "iconv('utf-16', 'utf-8', hex2str(\"$1\"))", $str);    
}
function hex2str($hex) {
    $r = '';
    for ($i = 0; $i < strlen($hex) - 1; $i += 2)
    $r .= chr(hexdec($hex[$i] . $hex[$i + 1]));
    return $r;
}

这篇关于如何使用PHP5正确显示\uXXXX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆