Conversion of php Chinese characters: Conversion of Unicode (UTF8) to GBK php Chinese characters has always been a hassle
This class has four built-in filters "& # [dec];", "& # x [hex];", "% u [hex]", "utf8 conversion"
It is convenient for users and supports custom filtering for their favorite operations.
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) +)/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 ){
/***** (Qiushuiwuhen 2002-8-15 )******/
$ Ret = ""; $ str = $ arr [1];
If (preg_match_all ("/% \ w {2}/", $ str, $ matches )){
For ($ I = 0; $ I $ Chr1 = hexdec (substr ($ matches [0] [$ I], 1 ));
$ Arr = array ("f0", "e0", "c0", "0 ");
For ($ j = 0; $ j Hexdec ($ arr [$ j]) break;
$ Chr = hexdec (substr ($ matches [0] [$ I], 1)-hexdec ($ arr [$ j]);
While (++ $ j $ 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;
}
}
Example
$ Qswh = new qswhU ("qswhU. php"); // if the file name is qswhU. php, save the parameter
Echo"
Without parameters (the default value is: & # [num];): "; <br/> echo" \ n ". $ qswh-> decode ("& # x4E2D; & # x6587; & # x41; & # x62; & # x63;"); <br/> echo "\ n ". $ qswh-> decode ("& #20013; & #25991; & #65; & #98; & #99 ;"); <br/> echo "\ n call built-in filtering (UTF transcoding ):". $ qswh-> decode ("% E4 % B8 % AD % E6 % 96% 87% 20! % 22% 23% 24% 26 & #39 ;() * % 2B % 2C % 2F % 3A % 3B % 3C % 3D % 3E % 3F % 40% 5B % 5D % 5E % 60% 7B % 7C % 7D ~ % 25Abc ", 1); <br/> echo" \ n call built-in filter unescape (% u [num]): ". $ qswh-> decode ("% u4E2D % u6587Abc", 2); <br/> echo "\ n Custom filtering ([x + num]):". $ qswh-> decode ("[x4E2D] [x6587] [x41] [x62] [x63]", "/\ [(\ w +) \]/"); </p> <p> The effect is as follows: <br/> without parameters (the default value is: & # [num];): <br/> Chinese Abc <br/> call built-in filtering (UTF transcoding): Chinese! "# $ % & #39; () * +,/:;<=>? @ [] ^ '{| }~ % Abc <br/> call built-in filter unescape (% u [num]): Chinese Abc <br/> Custom filter ([x + num]): Chinese Abc