当前位置:首页 > PHP教程 > php函数 > 列表

PHP实现Javascript中的escape及unescape函数代码分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-11 10:43:42 浏览: 评论:0 

这篇文章主要介绍了PHP实现Javascript中的escape及unescape函数代码分享,本文给出两个实现版本,需要的朋友可以参考下

这个类相当好用.作用么,PHP做JSON传递GBK字符,比如中文,日文,韩文神马的Unicode最合适不过了..

  1. <?php 
  2. classcoding 
  3.   //模仿JAVASCRIPT的ESCAPE和UNESCAPE函数的功能  
  4.   functionunescape($str
  5.   { 
  6.     $text=preg_replace_callback("/%u[0-9A-Za-z]{4}/",array
  7.       &$this
  8.       'toUtf8' 
  9.     ),$str); 
  10.     returnmb_convert_encoding($text,"gb2312","utf-8"); 
  11.   } 
  12.     
  13.   functiontoUtf8($ar
  14.   { 
  15.     foreach($aras$val){ 
  16.       $val=intval(substr($val,2),16); 
  17.       if($val<0x7F){// 0000-007F  
  18.         $c.=chr($val); 
  19.       }elseif($val<0x800){// 0080-0800  
  20.         $c.=chr(0xC0|($val/64)); 
  21.         $c.=chr(0x80|($val%64)); 
  22.       }else{// 0800-FFFF  
  23.         $c.=chr(0xE0|(($val/64)/64)); 
  24.         $c.=chr(0x80|(($val/64)%64)); 
  25.         $c.=chr(0x80|($val%64)); 
  26.       } 
  27.     } 
  28.     return$c
  29.   } 
  30.     
  31.   functionescape($string,$encoding='gb2312'
  32.   { 
  33.     $return=''
  34.     for($x=0;$x<mb_strlen($string,$encoding);$x++){ 
  35.       $str=mb_substr($string,$x,1,$encoding); 
  36.       if(strlen($str)>1){// 多字节字符  
  37.         $return.='%u'.strtoupper(bin2hex(mb_convert_encoding($str,'UCS-2',$encoding))); 
  38.       }else
  39.         $return.='%'.strtoupper(bin2hex($str)); 
  40.       } 
  41.     } 
  42.     return$return
  43.   } 
  44.     
  45.   functiongb2utf8($string,$encoding='utf-8',$from_encode='gb2312'
  46.   { 
  47.     returnmb_convert_encoding($string,$encoding,$from_encode); 
  48.   } 
  49.     
  50. ?> 

google code 上找到的另外一个类似脚本

  1. <?php 
  2.    
  3.     functionphpescape($str
  4.     { 
  5.         $sublen=strlen($str); 
  6.         $retrunString=""
  7.         for($i=0;$i<$sublen;$i++) 
  8.         { 
  9.             if(ord($str[$i])>=127) 
  10.             { 
  11.                 $tmpString=bin2hex(iconv("gbk","ucs-2",substr($str,$i,2))); 
  12.                 $tmpString=substr($tmpString,2,2).substr($tmpString,0,2); 
  13.                 $retrunString.="%u".$tmpString
  14.                 $i++; 
  15.             }else
  16.                 $retrunString.="%".dechex(ord($str[$i])); 
  17.             } 
  18.         } 
  19.         return$retrunString
  20.     } 
  21.    
  22.    
  23.     functionescape($str
  24.     { 
  25.         preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r); 
  26.         $ar=$r[0]; 
  27.         foreach($aras$k=>$v
  28.         { 
  29.             if(ord($v[0])<128) 
  30.                 $ar[$k]=rawurlencode($v); 
  31.             else 
  32.                 $ar[$k]="%u".bin2hex(iconv("UTF-8","UCS-2",$v)); 
  33.         } 
  34.         returnjoin("",$ar); 
  35.     } 
  36.    
  37.     functionphpunescape($source
  38.     { 
  39.         $decodedStr=""
  40.         $pos=0; 
  41.         $len=strlen($source); 
  42.           
  43.         while($pos<$len
  44.         { 
  45.             $charAt=substr($source,$pos,1); 
  46.             if($charAt=='%'
  47.             { 
  48.                 $pos++; 
  49.                 $charAt=substr($source,$pos,1); 
  50.                 if($charAt=='u'
  51.                 { 
  52.                     // we got a unicode character  
  53.                     $pos++; 
  54.                     $unicodeHexVal=substr($source,$pos,4); 
  55.                     $unicode=hexdec($unicodeHexVal); 
  56.                     $entity="&#".$unicode.';'
  57.                     $decodedStr.=utf8_encode($entity); 
  58.                     $pos+=4; 
  59.                 }else
  60.                     // we have an escaped ascii character  
  61.                     $hexVal=substr($source,$pos,2); 
  62.                     $decodedStr.=chr(hexdec($hexVal)); 
  63.                     $pos+=2; 
  64.                 } 
  65.             }else
  66.                 $decodedStr.=$charAt
  67.                 $pos++; 
  68.             } 
  69.         } 
  70.         return$decodedStr
  71.     } 
  72.       
  73.       
  74.     functionunescape($str
  75.     { 
  76.         $str=rawurldecode($str); 
  77.         preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r); 
  78.         $ar=$r[0]; 
  79.         #print_r($ar); 
  80.         foreach($aras$k=>$v
  81.         { 
  82.             if(substr($v,0,2)=="%u"
  83.                 $ar[$k]=iconv("UCS-2","UTF-8",pack("H4",substr($v,-4))); 
  84.             elseif(substr($v,0,3)=="&#x"
  85.                 $ar[$k]=iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1))); 
  86.             elseif(substr($v,0,2)=="&#"
  87.             { 
  88.                 //echo substr($v,2,-1).""; 
  89.                 $ar[$k]=iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1))); 
  90.             } 
  91.         } 
  92.         returnjoin("",$ar); 
  93.     } 
  94.    
  95. ?>

Tags: Javascript函数 escape unescape

分享到: