The escape () function can encode a string so that the string can be read on all computers. Syntax: escape (string) parameter: string required. description: the string to be escaped or encoded.
The escape () function can encode a string so that the string can be read on all computers.
Syntax:Escape (string)Parameters:String required,Description:The string to be escaped or encoded.
Return value:A copy of the encoded string. Some characters are replaced with hexadecimal escape sequences.
- Function phpescape ($ str)
- {
- $ Sublen = strlen ($ str );
- $ Retrunstring = "";
- For ($ I = 0; $ I <$ sublen; $ I ++)
- {
- If (ord ($ str [$ I]) >= 127)
- {
- $ Tmpstring = 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)Parameters:String required,Description:The string to be decoded or reversed.
Return value:String is a decoded copy.
Note: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.
- 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 );
- }