Functionphpescape ($ str) { $ Sublen = strlen ($ str ); $ RetrunString = ""; For ($ I = 0; $ I <$ sublen; $ I ++) { If (ord ($ str [$ I]) >= 127) { $ TmpString = bin2hex (iconv ("gbk", "UCS-2", substr ($ str, $ I, 2 ))); $ TmpString = substr ($ tmpString, 2, 2). substr ($ tmpString, 0, 2 ); $ RetrunString. = "% u". $ tmpString; $ I ++; } Else { $ RetrunString. = "%". dechex (ord ($ str [$ I]); } } Return $ retrunString; } Functionescape ($ str) { Preg_match_all ("/[\ x80-\ xff]. | [\ x01-\ x7f] +/", $ str, $ r ); $ Ar = $ r [0]; Foreach ($ aras $ k => $ v) { If (ord ($ v [0]) <128) $ Ar [$ k] = rawurlencode ($ v ); Else $ Ar [$ k] = "% u". bin2hex (iconv ("UTF-8", "UCS-2", $ v )); } Returnjoin ("", $ ar ); } Functionphpunescape ($ source) { $ DecodedStr = ""; $ Pos = 0; $ Len = strlen ($ source ); While ($ pos <$ len) { $ CharAt = substr ($ source, $ pos, 1 ); If ($ charAt = '% ') { $ Pos ++; $ CharAt = substr ($ source, $ pos, 1 ); If ($ charAt = 'u ') { // We got a unicode character $ Pos ++; $ UnicodeHexVal = substr ($ source, $ pos, 4 ); $ Unicode = hexdec ($ unicodeHexVal ); $ Entity = "& #". $ unicode .';'; $ DecodedStr. = utf8_encode ($ entity ); $ Pos + = 4; } Else { // We have an escaped ascii character $ HexVal = substr ($ source, $ pos, 2 ); $ DecodedStr. = chr (hexdec ($ hexVal )); $ Pos + = 2; } } Else { $ DecodedStr. = $ charAt; $ Pos ++; } } Return $ decodedStr; } Functionunescape ($ str) { $ Str = rawurldecode ($ str ); Preg_match_all ("/(? : % U. {4}) | & # x. {4}; | & # \ d +; |. +/U ", $ str, $ r ); $ Ar = $ r [0]; # Print_r ($ ar ); Foreach ($ aras $ 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) = "&#") { // Echo substr ($ v, 2,-1 ).""; $ Ar [$ k] = iconv ("UCS-2", "UTF-8", pack ("n", substr ($ v, 2,-1 ))); } } Returnjoin ("", $ ar ); } ?> |