The escape () function can encode a string so that the string can be read on all computers.
Syntax
Escape (string) parameter description
String is required. The string to be escaped or encoded.
Return value
A copy of the encoded string. Some characters are replaced with hexadecimal escape sequences.
Function php Tutorial escape ($ str)
{
$ Sublen = strlen ($ str );
$ Retrunstring = "";
For ($ I = 0; $ I <$ sublen; $ I ++)
{
If (ord ($ str [$ I]) >= 127)
{
$ Tmps tutorial tring = bin2hex (iconv ("gb2312", "UCS-2", substr ($ str, $ I, 2 )));
// $ Tmpstring = substr ($ tmpstring,). substr ($ tmpstring,); this option may be enabled in window
$ Retrunstring. = "% u". $ tmpstring;
$ I ++;
} Else {
$ Retrunstring. = "%". dechex (ord ($ str [$ I]);
}
}
Return $ retrunstring;
}
The unescape () function can decode strings encoded by escape.
Syntax
Unescape (string) parameter description
String is required. The string to be decoded or reversed.
Return value
String is a decoded copy.
Description
This function works like this: find the character sequence in the form of % xx and % uxxxx (x indicates a hexadecimal number ), decodes character sequences such as unicode u00xx and uxxxx.
Php processing
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", "gbk", 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 );
}