Home  >  Article  >  Backend Development  >  php escape URL encoding_PHP tutorial

php escape URL encoding_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:48:351002browse

Xu Zuning's PHP version of the escape/unescape function

Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319697.htmlTechArticleXu Zuning’s PHP version of the escape/unescape function copy code code is as follows: function escape($str) { preg_match_all(" /[x80-xff].|[x01-x7f]+/",$str,$r); $ar = $r[0]; foreach($ar as $k=$...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn