php来解析javascript函数escape编码过的字符串代码如下:
//对于在javascript中escape过的字符,直接用unescape方法
classescape_class
//编码
function escape($str,$TargetLang) {
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("UTF-8","UCS-2",$v));
$ar[$k] = "%u".bin2hex(iconv($TargetLang,"UCS-2",$v));
}
return join("",$ar);
}
//解码
function unescape($str,$TargetLang) {
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u" && strlen($v) == 6)
//$ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4)));
$ar[$k] = iconv("UCS-2",$TargetLang,pack("H4",substr($v,-4)));
}
return join("",$ar);
}
}