The conversion of Chinese characters in PHP has always been a tricky business.
The class has four filters "&#[dec];", "& #x [hex];", "%u[hex]", "UTF8 conversion"
Easy to use, but also can customize the filter to do their favorite operation
qswhu.php Download from here
Http://www.blueidea.com/user/qswh/qswhU.zip
Class qswhu{
var $qswhData;
function Qswhu ($filename = "qswhu.php") {
$this->qswhdata=file ($filename);
}
function decode ($STR, $pattern =0) {
$arr =array ("/&# (\w+);/iu", "/((%\w\w) +)/I", "/%u (\w{4,5})/iu");
if (Is_integer ($pattern)) {
if ($pattern >=count ($arr)) Die ("Invalid Function");
$pattern = $arr [$pattern];
}
Return Preg_replace_callback ($pattern, Array ($this, "U2GB"), $STR);
}
function U2GB ($arr) {
$ret = ""; $str = $arr [1];
if (Preg_match_all ("/%\w{2}/", $str, $matches)) {
For ($i =0 $i <count ($matches [0]); $i + +) {
$CHR 1=hexdec (substr ($matches [0][$i],1));
$arr =array ("F0", "E0", "C0", "0");
For ($j =0 $j <count ($arr); $j + +) if ($chr 1>hexdec ($arr [$j])) break;
$CHR =hexdec (substr ($matches [0][$i],1))-hexdec ($arr [$j]);
while (+ + $j <count ($arr)) $CHR = $chr *0x40+ (Hexdec (substr ($matches [0][++ $i],1)) -0x80);
$str =dechex ($CHR);
if (strlen ($STR) ==4) {
$p =hexdec (substr ($str, 0,2)) -0x4d;
$q =hexdec (substr ($STR, 2)) *4;
$ret. =CHR (Hexdec (substr ($this->qswhdata[$p], $q, 2));
$ret. =CHR (Hexdec (substr ($this->qswhdata[$p), $q +2,2));
}else
$ret. =CHR (Hexdec ($STR));
}
}
else{
if (Strtolower ($str [0]) = = "X")
$str =substr ($STR, 1);
Else
if (strlen ($STR)!=4) $str =dechex ($STR);
if (strlen ($STR) ==4) {
$p =hexdec (substr ($str, 0,2)) -0x4d;
$q =hexdec (substr ($STR, 2)) *4;
$ret. =CHR (Hexdec (substr ($this->qswhdata[$p], $q, 2));
$ret. =CHR (Hexdec (substr ($this->qswhdata[$p), $q +2,2));
}else
$ret. =CHR (Hexdec ($STR));
}
return $ret;
}
}