首页 > 开发 > PHP > 正文

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

2024-05-04 23:31:09
字体:
来源:转载
供稿:网友

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

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

 

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

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

 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表