本条类蛮好用.功效么,PHP做JSON传递GBK字符,举个例子中文,保加利亚共和国(The Republic of Bulgaria)语,马耳他语神马的Unicode最合适不过了..

<?    
function phpescape($str) 

         $sublen=strlen($str); 
         $retrunString=””; 
         for ($i=0;$i<$sublen;$i++) 
         { 
                  if(ord($str[$i])>=127) 
                  { 
                           $tmpString=bin2hex(iconv(“gb2312″,”ucs-2”,substr($str,$i,2))); 
                           //$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);window下可能要开拓此项 
                           $retrunString.=”%u”.$tmpString; 
                           $i++; 
                  } else { 
                           $retrunString.=”%”.dechex(ord($str[$i])); 
                  } 
         } 
         return $retrunString; 
}  
function unescape($str) { 
         $str = rawurldecode($str); 
         preg_js中的escape及unescape函数的php实现代码,函数代码。match_all(“/%u.{4}|.{4};|\d+;|.+/U”,$str,$r); 
         $ar = $r[0]; 
         foreach($ar as $k=>$v) { 
                  if(substr($v,0,2) == “%u”) 
                           $ar[$k] = iconv(“UCS-2″,”GBK”,pack(“H4”,substr($v,-4))); 
                  elseif(substr($v,0,3) == “”) 
                           $ar[$k] = iconv(“UCS-2″,”GBK”,pack(“H4”,substr($v,3,-1))); 
                  elseif(substr($v,0,2) == “”) { 
                           $ar[$k] = iconv(“UCS-2″,”GBK”,pack(“n”,substr($v,2,-1))); 
                  } 
         } 
         return join(“”,$ar); 
}    
?>

这里,一般都急需事先将健康的字符串编码成 JavaScript unescape()
函数能够解译的格式,以 PHP 为例,能够使用以下函数实现 Javascript 中
escape() 函数同样的效果:

前端开荒程序猿都精通javascript有编码函数escape()和呼应的解码函数unescape(),而php中独有个urlencode和urldecode,这些编码和平消除码函数对encodeU奥迪Q7I和encodeU奥德赛IComponent有效,不过对escape的是无效的。
javascript中的escape()函数和unescape()函数用户字符串编码,类似于PHP中的urlencode()函数,上面是php实现的escape函数代码:

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

}
?>

functionphpescape($str) { $sublen=strlen($str);
$retrunString=””; for($i=0;$i$sublen;$i++) { if(ord($str[$i])=127) {
$tmpString=bin2hex(iconv(“gb2312″,”ucs-2”,substr($str,$i,2)));…

复制代码 代码如下:

复制代码 代码如下:

google code 上找到的别的二个周边脚本

<?php
function escapeToHex($string, $encoding = UTF-8) {
$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;
}
?>

/**
 * js escape php 实现
 * @param $string           the sting want to be escaped
 * @param $in_encoding      
 * @param $out_encoding     
 */
function escape($string, $in_encoding = ‘UTF-8’,$out_encoding =
‘UCS-2’) {
    $return = ”;
    if (function_exists(‘mb_get_info’)) {
        for($x = 0; $x < mb_strlen ( $string, $in_encoding ); $x
++) {
            $str = mb_substr ( $string, $x, 1, $in_encoding );
            if (strlen ( $str ) > 1) { // 多字节字符
                $return .= ‘%u’ . strtoupper ( bin2hex (
mb_convert_encoding ( $str, $out_encoding, $in_encoding ) ) );
            } else {
                $return .= ‘%’ . strtoupper ( bin2hex ( $str ) );
            }
        }
    }
    return $return;
}

网站地图xml地图