- // Php Chinese character transcoding
- 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 );
- } // Programmer's house bbs.it-home.org
- 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;
- }
- }
Instance used:
- $ Qswh = new qswhU ("qswhU. php"); // if the file name is qswhU. php, save the parameter
- Echo" Without parameters (the default value is: & # [num];): "; <li> echo"/n ". $ qswh-> decode ("& # x4E2D; & # x6587; & # x41; & # x62; & # x63;"); <li> echo "/n ". $ qswh-> decode ("& #20013; & #25991; & #65; & #98; & #99 ;"); <li> echo "/n call built-in filtering (UTF transcoding ):". $ qswh-> decode ("% E4 % B8 % AD % E6 % 96% 87% 20! % 22% 23% 24% 25% 26 '() * % 2B % 2C % 2F % 3A % 3B % 3C % 3D % 3E % 3F % 40% 5B % 5D % 5E % 60% 7B % 7C % 7D ~ % 25Abc ", 1); <li> echo"/n call built-in filter unescape (% u [num]): ". $ qswh-> decode ("% u4E2D % u6587Abc", 2); <li> echo "/n Custom filtering ([x + num]):". $ qswh-> decode ("[x4E2D] [x6587] [x41] [x62] [x63]", "// [(/w +)/]/"); </ol> </p> <em onclick = "copycode ($ ('code _ HJP ')); "> </em> </p> <p> The effect is as follows: without parameters (the default value is: & # [num];): chinese Abc call built-in filtering (UTF transcoding): Chinese! "# $ % & '() * +,/:; <=>? @ [] ^ '{| }~ % Abc call built-in filter unescape (% u [num]): Chinese Abc custom filter ([x + num]): chinese Abc </p> </td> </tr> </table> </p> <p id = "comment_46682" class = "cm"> </p> <p id = "post_rate_p_46682">
|