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

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

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

  1. <?php
  2. classcoding
  3. {
  4. //模仿JAVASCRIPT的ESCAPE和UNESCAPE函数的功能
  5. functionunescape($str)
  6. {
  7. $text=preg_replace_callback("/%u[0-9A-Za-z]{4}/",array(
  8. &$this,
  9. 'toUtf8'
  10. ),$str);
  11. returnmb_convert_encoding($text,"gb2312","utf-8");
  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. functionescape($string,$encoding='gb2312')
  31. {
  32. $return='';
  33. for($x=0;$x<mb_strlen($string,$encoding);$x++){
  34. $str=mb_substr($string,$x,1,$encoding);
  35. if(strlen($str)>1){// 多字节字符
  36. $return.='%u'.strtoupper(bin2hex(mb_convert_encoding($str,'UCS-2',$encoding)));
  37. }else{
  38. $return.='%'.strtoupper(bin2hex($str));
  39. }
  40. }
  41. return$return;
  42. }
  43. functiongb2utf8($string,$encoding='utf-8',$from_encode='gb2312')
  44. {
  45. returnmb_convert_encoding($string,$encoding,$from_encode);
  46. }
  47. }
  48. ?>

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

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