复制代码 代码如下:
function js_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;}
复制代码 代码如下:
function phpescape($str)
{
$sublen=strlen($str);
$retrunString="";
for ($i=0;$i<$sublen;$i++)
{
if(ord($str[$i])>=127)
{
$tmpString=bin2hex(iconv("gb2312","ucs-2",substr($str,$i,2)));
//$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);window下可能要打开此项
$retrunString.="%u".$tmpString;
$i++;
} else
{
$retrunString.="%".dechex(ord($str[$i]));
}
}
return $retrunString;
}
复制代码 代码如下:
function phpEscape($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);
}
复制代码 代码如下:
$ar = $r[0];//$r[0]里存放是匹配到的数组
foreach($ar as $k=>$v) {
if(ord($v[0]) < 128)//假如字符编码值小于128,说明是个英文字符
$ar[$k] = rawurlencode($v); //直接用rawurlencode编码
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v)); //否则的话用iconv函数把汉字转变成ucs-2编码,也就是unicode编码
}
复制代码 代码如下:
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/%u.{4}|.{4};|/d+;|.+/U",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u")
$ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,-4)));
elseif(substr($v,0,3) == "")
$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);
}
新闻热点
疑难解答