PHP escape unescape

180it 2021-02-28 PM 1862℃ 0条

/ PHP escape /
public static 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(NUll,"UCS-2",$v));
    }
}
return join("",$ar);

}

/ PHP unescape /
public static 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",NULL,pack("H4",substr($v,-4)));
    }
}
return join("",$ar);

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP escape unescape