2009年3月28日

【PHP】PHP 中的 unescape 函式

   function phpescape($str){

    
$sublen=strlen($str);

    
$reString="";

    
for ($i=0;$i<$sublen;$i++){

        
if(ord($str[$i])>=127){

            
$tmpString=bin2hex(iconv("utf-8","ucs-2",substr($str,$i,2)));    



            
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 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); 









轉貼至:

http://www.phpchina.com/html/91/n-1091.html

沒有留言:

張貼留言