| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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); } ?> |