帝国cms二次开发:结合项url参数优化-让URL地址看起来更美!

2018-02-0105:15:14网站建设与开发 后端程序开发Comments4,029 views字数 1475阅读模式

帝国官方结合项如果同一个字段不同值点击两次以上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文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

优化后是不是感觉清爽了很多文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

优化方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

在官方的结合项的函数方法 echo $allstr; 这行代码的前面插入以下代码即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html


//新增代码---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]);
}
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

$newurl = $parts[path].'?'.http_build_query($output);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

$allstr = str_replace('href="'.$url.'"', 'href="'.$newurl.'"',  $allstr);
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

//新增代码---jimmy.end-----文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html


如果有多个结合项,可以把上面的代码放到一个函数里面调用方法就更加简单了,只需把  echo $allstr; 替换成  echo beautifyUrl($allstr); 即可文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html


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]);
}
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

$newurl = $parts[path].'?'.http_build_query($output);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

$allstr = str_replace('href="'.$url.'"', 'href="'.$newurl.'"',  $allstr);
}
//新增代码---jimmy.end-----
return  $allstr;
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

感谢作者:jimmy 提供的方案!亲测可行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/195.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/cms/195.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定