The conversion of php Chinese characters has always been quite troublesome. This class has four built-in functions: htmlHex, htmlDec, escape, and u2utf8 for your convenience. you can also customize functions to perform your favorite operations on qswhGBK. php download http://www.blueidea.com/user/qswh/qswhGBK.zip from here
Converting Chinese characters in php has always been a problem.
This class has four built-in functions: "htmlHex", "htmlDec", "escape", "u2utf8"
It is convenient for you to use, and you can also customize functions for your favorite operations.
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 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;
}
}
Example
$ Words = "Chinese Abc ";
Function ex ($ str) {return "[". $ str. "]";}
$ Qswh = new qswhGBK ("qswhGBK. php"); // if the file name is qswhGBK. php, save the echo parameter (" Without parameters :". $ qswh-> gb2u ($ words); echo ("Call the built-in function htmlHex :". $ qswh-> gb2u ($ words, 1); echo ("Call the built-in function htmlDec :". $ qswh-> gb2u ($ words, 2); echo ("Call the built-in function escape :". $ qswh-> gb2u ($ words, 3); echo ("Call the built-in function u2utf8 :". $ qswh-> gb2u ($ words, 4); echo ("call a UDF :". $ qswh-> gb2u ($ words, ex); effect: without parameters: 4E2D6587416263 call the built-in function htmlHex: & # x4E2D; & # x6587; & # x41; & # x62; & # x63; call the built-in function htmlDec: & #20013; & #25991; & #65; & #98; & #99; call the built-in function escape: % u4E2D % u6587Abc call built-in function u2ut F8: % e4 % b8 % ad % e6 % 96% 87Abc call the custom function: [4E2D] [6587] [41] [62] [63] ps. this article is only available in the csdn document center. if you want to reprint it, contact the author. Otherwise, you will be responsible for the consequences.