chriszg 发表于 2017-3-22 10:33:34

php escape URL 编码

php escape URL 编码
标签:   escape   unescape   rawurlencode   bin2hex   
php提供的URL编码函数是基于字节的,对由ie的javascript函数escape编码的数据就无能为力了。
徐祖宁的php版的escape/unescape函数

functionescape($str){
preg_match_all("/.|+/",$str,$r);
$ar=$r;
foreach($aras$k=>$v){
if(ord($v)<128)
$ar[$k]=rawurlencode($v);
else
$ar[$k]="%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
returnjoin("",$ar);
}

functionunescape($str){
$str=rawurldecode($str);
preg_match_all("/(?:%u.{4})|.+/",$str,$r);
$ar=$r;
foreach($aras$k=>$v){
if(substr($v,0,2)=="%u"&&strlen($v)==6)
$ar[$k]=iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
}
returnjoin("",$ar);
}

转自:
http://www.5do8.com/code/lamp/PHP/198.html
页: [1]
查看完整版本: php escape URL 编码