由于需要用到php调用js文件,在网上找了相关的资料,并改写了相关的方法。 php实现 Javascript的escape函数方法: function escape($str) { preg_match_all("/[/xc2-/xdf][/x80-/xbf]+|[/xe0-/xef][/x80-/xbf]{2}|[/xf0-/xff][/x80-/xbf]{3}|[/x01-/x7f]+/e",$str,$r); //匹配utf-8字符 $str = $r[0]; $l = count($str); for($i=0; $i <$l; $i++) { $value = ord($str[$i][0]); if($value < 223) { $str[$i] = rawurlencode($str[$i]); } else { $str[$i] = "%u".strtoupper(bin2hex(mb_convert_encoding($str[$i],"UCS-2","UTF-8"))); } } return join("",$str); } php实现 Javascript的unescape函数方法: 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,0,2) == "%u") { //$ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4))); $ar[$k] = mb_convert_encoding(pack("H4",substr($v,-4)),"UTF-8","UCS-2"); } elseif(substr($v,0,3) == "&#x") { //$ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1))); $ar[$k] = mb_convert_encoding(pack("H4",substr($v,3,-1)),"UTF-8","UCS-2"); } elseif(substr($v,0,2) == "&#") { //$ar[$k] = iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1))); $ar[$k] = mb_convert_encoding(pack("n",substr($v,2,-1)),"UTF-8","UCS-2"); } } return join("",$ar); }