PHP下escape解码函数的实现方法

前端之家收集整理的这篇文章主要介绍了PHP下escape解码函数的实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

GB2312编码:
<div class="codetitle"><a style="CURSOR: pointer" data="79948" class="copybut" id="copybut79948" onclick="doCopy('code79948')"> 代码如下:

<div class="codebody" id="code79948">
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,2) == "%u")
$ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,-4)));
elseif(substr($v,3) == "&#x")
$ar[$k] = iconv("UCS-2",3,-1)));
elseif(substr($v,2) == "&#") {
$ar[$k] = iconv("UCS-2",pack("n",2,-1)));
}
}
return join("",$ar);
}

UTF8编码:
<div class="codetitle"><a style="CURSOR: pointer" data="71662" class="copybut" id="copybut71662" onclick="doCopy('code71662')"> 代码如下:
<div class="codebody" id="code71662">
function unescape($str){
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++){
if ($str[$i] == '%' && $str[$i+1] == 'u'){
$val = hexdec(substr($str,$i+2,4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}
else if ($str[$i] == '%'){
$ret .= urldecode(substr($str,$i,3));
$i += 2;
}
else $ret .= $str[$i];
}
return $ret;
}

猜你在找的PHP相关文章