PHP实现Javascript中的escape及unescape函数代码分享
发布:smiling 来源: PHP粉丝网 添加日期:2021-05-11 10:43:42 浏览: 评论:0
这篇文章主要介绍了PHP实现Javascript中的escape及unescape函数代码分享,本文给出两个实现版本,需要的朋友可以参考下
这个类相当好用.作用么,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})|&#x.{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)=="&#x")
- $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);
- }
- ?>
Tags: Javascript函数 escape unescape
相关文章
- ·javascript unescape() escape() 字符编码解码函数(2013-11-28)
- ·js的escape转义中文php的转换函数(2013-12-27)
- ·php中escape unescape的写法 (2014-08-25)
- ·php mysql_real_escape_string()函数(2014-09-11)
- ·mysql_real_escape_string(2014-09-12)
- ·PHP中addslashes与mysql_escape_string的区别分析(2019-08-18)
- ·mysql_escape_string()函数用法分析(2019-08-22)
- ·PHP函数addslashes和mysql_real_escape_string的区别(2020-11-19)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)