感謝作者:jimmy 提供的方案!這種正則過了方法墨魚親測可行!
帝國官方結合項如果同一個字段不同值點擊兩次以上url就會出現重復的參數,當然對功能是沒有影響的,感覺不大美觀!
優化前:/e/action/ListInfo.php?&classid=19&orderby=psalenum&myorder=1&ph=1&classid=19&pbrand=27&orderby=psalenum&myorder=1&myorder=1
優化后:/e/action/ListInfo.php?classid=19&orderby=psalenum&myorder=1&ph=1&pbrand=27
優化后是不是感覺清爽了很多
優化方法:
在官方的結合項的函數方法 echo $allstr; 這行代碼的前面插入以下代碼即可:
//新增代碼---jimmy.start-----
preg_match_all ('/<a href=/"(.*?)/".*?>(.*?)<//a>/i',$allstr,$matches);
for($i=0;$i<count($matches[1]);$i++){
$url = $matches[1][$i];
$parts = parse_url($url);
$query = explode('&', $parts[query]);
$output=array();
foreach ($query as $key => $value) {
if($value){
$params = explode('=', $value);
if($params[0]){
$output[$params[0]]=urldecode($params[1]);
}
}
}
$newurl = $parts[path].'?'.http_build_query($output);
$allstr = str_replace('href="'.$url.'"', 'href="'.$newurl.'"', $allstr);
}
//新增代碼---jimmy.end-----
如果有多個結合項,可以把上面的代碼放到一個函數里面調用方法就更加簡單了,只需把 echo $allstr; 替換成 echo beautifyUrl($allstr); 即可
function beautifyUrl($allstr){
//新增代碼---jimmy.start-----
preg_match_all ('/<a href=/"(.*?)/".*?>(.*?)<//a>/i',$allstr,$matches);
for($i=0;$i<count($matches[1]);$i++){
$url = $matches[1][$i];
$parts = parse_url($url);
$query = explode('&', $parts[query]);
$output=array();
foreach ($query as $key => $value) {
if($value){
$params = explode('=', $value);
if($params[0]){
$output[$params[0]]=urldecode($params[1]);
}
}
}
$newurl = $parts[path].'?'.http_build_query($output);
$allstr = str_replace('href="'.$url.'"', 'href="'.$newurl.'"', $allstr);
}
//新增代碼---jimmy.end-----
return $allstr;
}
新聞熱點
疑難解答