Chinese Character | conversion
The conversion of Chinese characters in PHP has always been a tricky business.
The class is built with four functions "Htmlhex", "Htmldec", "Escape", "U2utf8"
Easy to use, but also can customize functions to do their favorite operation
qswhgbk.php Download from here
Http://www.blueidea.com/user/qswh/qswhGBK.zip
?
Class qswhgbk{
var $qswhData;
function QSWHGBK ($filename = "qswhgbk.php") {
$this->qswhdata=file ($filename);
}
function gb2u ($GB, $callback = "") {
/****** (Qiushuiwuhen 2002-8-15) ******/
$ret = "";
for ($i =0; $i <strlen ($GB); $i + +) {
if ($p =ord (substr ($GB, $i, 1)) >127) {
$q =ord (substr ($GB, + + $i, 1));
$q = ($q-($q >128?65:64)) *4;
$q =substr ($this->qswhdata[$p -128], $q, 4);
}
Else
$q =dechex ($p);
if (empty ($callback))
$ret. = $q;
else {
$arr =array ("Htmlhex", "Htmldec", "Escape", "U2utf8");
if (Is_integer ($callback)) {
if ($callback >count ($arr)) Die ("Invalid Function");
$ret. = $this-> $arr [$callback-1] ($q);
}else
$ret. = $callback ($q);
}
}
return $ret;
}
function Htmlhex ($STR) {
Return "& #x". $str. ";
}
function Htmldec ($STR) {
Return "&#". Hexdec ($STR). ";";
}
function Escape ($STR) {
Return Hexdec ($STR) <256?chr (Hexdec ($STR)): "%u". $str;
}
function U2utf8 ($STR) {
/****** (Qiushuiwuhen 2002-8-15) ******/
$SP = "!" () *-.0123456789abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz~ ";
$dec =hexdec ($STR);
$bin =decbin ($DEC);
$len =strlen ($bin);
$arr =array ("C0", "E0", "F0");
if ($dec >0x7f) {
$ret = "";
for ($i = $len, $j =-1 $i >=0; $i-=6, $j + +) {
if ($i >6)
$ret = "%". Dechex (0x80+bindec (substr ($bin, $i -6,6)). $ret;
Else
$ret = "%". Dechex (Hexdec ($arr [$j]) +bindec (substr ($bin, 0,6-$i)). $ret;
}
}else{
if (Strpos ($SP, Chr ($DEC))
$ret =CHR ($DEC);
Else
$ret = "%" Strtolower ($STR);
}
return $ret;
}
}
Usage examples
$words = "Chinese abc";
function Ex ($STR) {return "[". $str. "]";}
$QSWH =new QSWHGBK ("qswhgbk.php"); If the file name is qswhgbk.php, you can save the parameter
Echo ("<xmp> without Parameters:". $qswh->gb2u ($words));
Echo ("\ n call built-in function Htmlhex:". $qswh->gb2u ($words, 1));
Echo ("\ n call built-in function Htmldec:". $qswh->gb2u ($words, 2));
Echo ("\ n call built-in function escape:". $qswh->gb2u ($words, 3));
Echo ("\ n call built-in function U2utf8:". $qswh->gb2u ($words, 4));
Echo ("\ n Call Custom Function:". $QSWH->gb2u ($words, ex));
The effect is as follows:
Without parameters: 4e2d6587416263 calls built-in functions Htmlhex: Chinese ABC calls built-in functions Htmldec: Chinese ABC calls built-in functions ESCAPE:%U4E2D%U6587ABC calls built-in functions U2utf8:%e4%b8%ad%e6 %96%87ABC Invoke Custom functions: [4e2d][6587][41][62][63]