<?php
function escape($str){
$res = @unpack("H*",iconv("utf-8","UCS-2",$str));
$res = preg_replace("/(.{4})/is","%u//1",$res[1]);
return $res;
}
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)));
elseif(substr($v,0,3) == "&#x")
$ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
elseif(substr($v,0,2) == "&#") {
$ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}
function escape1($str){
$sublen=strlen($str);
$reString="";
for ($i=0;$i<$sublen;$i++){
if(ord($str[$i])>=127){
$tmpString=bin2hex(iconv("GBK","ucs-2",substr($str,$i,2))); //此处GBK为目标代码的编码格式,请实际情况修改
if (!eregi("WIN",PHP_OS)){
$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);
}
$reString.="%u".$tmpString;
$i++;
} else {
$reString.="%".dechex(ord($str[$i]));
}
}
return $reString;
}
function unescape1($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] =!eregi("WIN",PHP_OS)?iconv("UCS-2","GBK",strrev(pack("H4",substr($v,-4)))):iconv("UCS-2","gb2312",pack("H4",substr($v,-4)));
elseif(substr($v,0,3) == "&#x")
$ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,3,-1)));
elseif(substr($v,0,2) == "&#") {
$ar[$k] = iconv("UCS-2","GBK",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}
?>
注意escape1和unescape1中对windows和unix的区分!