<? PHP
Function escape ($ Str ){
$ Res = @ unpack ("H *", iconv ("UTF-8", "UCS-2", $ Str ));
$ Res = preg_replace ("/(. {4})/is", "% u // 1", $ res [1]);
Return $ res;
}
Function Unescape ($ Str ){
$ STR = rawurldecode ($ Str );
Preg_match_all ("/% u. {4} | & # X. {4}; | & #/d +; |. +/u", $ STR, $ R );
$ AR = $ R [0];
Foreach ($ Ar as $ k => $ v ){
If (substr ($ V, 0, 2) = "% u ")
$ Ar [$ K] = iconv ("UCS-2", "UTF-8", pack ("H4", substr ($ V,-4 )));
Elseif (substr ($ V, 0, 3) = "& # X ")
$ Ar [$ K] = iconv ("UCS-2", "UTF-8", pack ("H4", substr ($ V, 3,-1 )));
Elseif (substr ($ V, 0, 2) = "&#"){
$ Ar [$ K] = iconv ("UCS-2", "UTF-8", pack ("N", substr ($ V, 2,-1 )));
}
}
Return join ("", $ AR );
}
Function escape1 ($ Str ){
$ Sublen = strlen ($ Str );
$ Restring = "";
For ($ I = 0; $ I <$ sublen; $ I ++ ){
If (ord ($ STR [$ I]) >= 127 ){
$ Tmpstring = bin2hex (iconv ("GBK", "ucs-2", substr ($ STR, $ I, 2); // here GBK is the encoding format of the target code, modify the actual situation
If (! Eregi ("win", php_ OS )){
$ Tmpstring = substr ($ tmpstring, 2, 2). substr ($ tmpstring, 0, 2 );
}
$ Restring. = "% u". $ tmpstring;
$ I ++;
} Else {
$ Restring. = "%". dechex (ord ($ STR [$ I]);
}
}
Return $ restring;
}
Function unescape1 ($ Str ){
$ STR = rawurldecode ($ Str );
Preg_match_all ("/% u. {4} | & # X. {4}; | & #/d +; |. +/u", $ STR, $ R );
$ AR = $ R [0];
Foreach ($ Ar as $ k => $ v ){
If (substr ($ V, 0, 2) = "% u ")
$ Ar [$ K] =! Eregi ("win", php_ OS )? Iconv ("UCS-2", "GBK", strrev (pack ("H4", substr ($ V,-4): iconv ("UCS-2", "gb2312 ", pack ("H4", substr ($ V,-4 )));
Elseif (substr ($ V, 0, 3) = "& # X ")
$ Ar [$ K] = iconv ("UCS-2", "GBK", pack ("H4", substr ($ V, 3,-1 )));
Elseif (substr ($ V, 0, 2) = "&#"){
$ Ar [$ K] = iconv ("UCS-2", "GBK", pack ("N", substr ($ V, 2,-1 )));
}
}
Return join ("", $ AR );
}
?> Note the differences between windows and UNIX in escape1 and unescape1!