欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-16 21:53:54
这个类相当好用.作用么,php做json传递gbk字符,比如中文,日文,韩文神马的unicode最合适不过了..

这个类相当好用.作用么,php做json传递gbk字符,比如中文,日文,韩文神马的unicode最合适不过了..

<?php
classcoding
{
  //模仿javascript的escape和unescape函数的功能 
  functionunescape($str)
  {
    $text=preg_replace_callback("/%u[0-9a-za-z]{4}/",array(
      &$this,
      'toutf8'
    ),$str);
    returnmb_convert_encoding($text,"gb2312","utf-8");
  }
  
  functiontoutf8($ar)
  {
    foreach($aras$val){
      $val=intval(substr($val,2),16);
      if($val<0x7f){// 0000-007f 
        $c.=chr($val);
      }elseif($val<0x800){// 0080-0800 
        $c.=chr(0xc0|($val/64));
        $c.=chr(0x80|($val%64));
      }else{// 0800-ffff 
        $c.=chr(0xe0|(($val/64)/64));
        $c.=chr(0x80|(($val/64)%64));
        $c.=chr(0x80|($val%64));
      }
    }
    return$c;
  }
  
  functionescape($string,$encoding='gb2312')
  {
    $return='';
    for($x=0;$x<mb_strlen($string,$encoding);$x++){
      $str=mb_substr($string,$x,1,$encoding);
      if(strlen($str)>1){// 多字节字符 
        $return.='%u'.strtoupper(bin2hex(mb_convert_encoding($str,'ucs-2',$encoding)));
      }else{
        $return.='%'.strtoupper(bin2hex($str));
      }
    }
    return$return;
  }
  
  functiongb2utf8($string,$encoding='utf-8',$from_encode='gb2312')
  {
    returnmb_convert_encoding($string,$encoding,$from_encode);
  }
  
}
?>

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

<?php
 
    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})|.{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)=="")
                $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);
    }
 
?>