php里实现javascript的unescape函数的功能

admin 发表于 [PHP] 分类,标签: PHP代码 PHP自定义函数
0
//加密函数
function  escape($str)  {
preg_match_all("/[\x80-\xff].  ¦[\x01-\x7f]+/",$str,$r);
$ar  =  $r[0];
foreach($ar  as  $k=>$v)  {
if(ord($v[0])  <  128)
$ar[$k]  =  rawurlencode($v);
else
$ar[$k]  =  "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return  join("",$ar);
}
//解密函数
function  unescape($str)  {
$str  =  rawurldecode($str);
preg_match_all("/(?:%u.{4})  &brvbar.+/",$str,$r);
$ar  =  $r[0];
foreach($ar  as  $k=>$v)  {
if(substr($v,0,2)  ==  "%u"  &&  strlen($v)  ==  6)
$ar[$k]  =  iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
}
return  join("",$ar);
}
要求iconv函数可用(iconv扩展库已加载),否则需要自行编程查表

发表我的评论