php escape URL编码

前端之家收集整理的这篇文章主要介绍了php escape URL编码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

徐祖宁的PHP版的escape/unescape函数
<div class="codetitle"><a style="CURSOR: pointer" data="59507" class="copybut" id="copybut59507" onclick="doCopy('code59507')"> 代码如下:

<div class="codebody" id="code59507">
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})|.+/",$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,2) == "%u" && strlen($v) == 6)
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
}
return join("",$ar);
}

猜你在找的PHP相关文章