PHP下escape解码函数的实现方法_php技巧
程序员文章站
2022-05-02 10:33:18
...
GB2312编码:
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);
}
UTF8编码:
function unescape($str){
$ret = '';
$len = strlen($str);
for ($i = 0; $i if ($str[$i] == '%' && $str[$i+1] == 'u'){
$val = hexdec(substr($str, $i+2, 4));
if ($val else if($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 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);
}
UTF8编码:
复制代码 代码如下:
function unescape($str){
$ret = '';
$len = strlen($str);
for ($i = 0; $i if ($str[$i] == '%' && $str[$i+1] == 'u'){
$val = hexdec(substr($str, $i+2, 4));
if ($val else if($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;
}
上一篇: php foreach正序倒序输出示例代码_PHP
下一篇: php中流行的rpc框架详解
推荐阅读
-
用js进行url编码后用php反解以及用php实现js的escape功能函数总
-
php中实现xml与mysql数据相互转换的方法_php技巧
-
php抓取网站图片并保存的实现方法_php技巧
-
PHP函数可变参数列表的具体实现方法介绍_PHP教程
-
详解PHP中的mb_detect_encoding函数使用方法_php技巧
-
php通过ksort()函数给关联数组按照键排序的方法_php技巧
-
php实现转换ubb代码的方法_php技巧
-
php的chr和ord函数实现字符加减乘除运算实现代码_php技巧
-
基于JS实现PHP的sprintf函数实例_javascript技巧
-
LINUX下PHP程序实现WORD文件转化为PDF文件的方法