PHP语法 截取显示指定字符串中两个字符中间的内容

2024-11-02 10:21:52
推荐回答(3个)
回答1:

function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u"){
$ar[$k] = mb_convert_encoding(pack("H4",substr($v,-4)),"gb2312","UCS-2");
}elseif(substr($v,0,3) == "&#x"){
$ar[$k] = mb_convert_encoding(pack("H4",substr($v,3,-1)),"gb2312","UCS-2");
}elseif(substr($v,0,2) == "&#") {
$ar[$k] = mb_convert_encoding(pack("H4",substr($v,2,-1)),"gb2312","UCS-2");
}
}
return join("",$ar);
}

$str = '/?y=%u597D&m=%u554A&h=%20';
$query = parse_url($str,PHP_URL_QUERY);
parse_str($query);
echo unescape($y);
?>

回答2:

$str="/?y=%u597D&m=%u554A&h=%20";
$result="";
if(preg_match('#y=(.+)&m=#isU',$str,$ma)){$result=unescape($ma[1]);}
echo $result;
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);
}

回答3:

你这个应该是对url 的拆分,拆倒是能拆。不过意义不大啊