PHP:如何删除URL中的重复的论点? [英] PHP: How to remove duplicate arguments in a url?

查看:232
本文介绍了PHP:如何删除URL中的重复的论点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个产品过滤器扩展Opencart的。

我指定大小,颜色等选项,像这样的网址:


  ?

index.php的路径=产品/类别和放大器;路径= 59_63&安培;大小= 57安培;颜色=黑色


问题是,当我点击链接是这样在页面上另一种颜色:


  

index.php的路径=产品/类别和放大器;路径= 59_63&安培;大小= 57安培;颜色=黑色&安培;色泽褐=


正如你可以看到有重复的颜色参数,它搅乱了类上市。
如何删除相同的参数,如果有?

原来Opencart的的链接建设者功能:

 公共职能的链接($路线,的$ args ='',$连接='NONSSL'){
    如果($连接=='NONSSL'){
        $ URL = $这个 - >网址;
    }其他{
        $ URL = $这个 - > SSL;
    }    $ URL =的index.php?路径=。 $路线;    如果(的$ args){
        $ URL = str_replace函数。('和;','&放大器;放大器;','&放大器;'LTRIM(的$ args。'和;'));
    }    返回$这个 - >重写($网址);
}


解决方案

有没有足够的信息来提供正确的答案,但我会采取一种猜测。

这个问题似乎与的$ args 。看来你是从URL以的$ args 并向其追加新的颜色参数。

如果URL是的index.php路线=产品/类别和放大器;路径= 59_63&安培;大小= 57安培;颜色=黑色,那么的$ args 路径= 59_63&安培;大小= 57安培;颜色=黑色

您追加到它颜色=棕色的$ args变得路径= 59_63&安培;大小= 57安培;颜色=黑色&安培;颜色=棕色

如果是这样的话,你可以做这样的事情:

  parse_str(的$ args,$ url_params);
$ url_params ['颜色'] ='棕'; // - 覆盖颜色=黑色颜色=棕色
的$ args = http_build_query($ url_params);

再通的$ args你的链接()功能。

I'm trying to write a product filter extension for opencart.

I assign size, color etc. options to the url like this:

index.php?route=product/category&path=59_63&size=57&color=black

The problem is when I click another color on the page the link goes like this:

index.php?route=product/category&path=59_63&size=57&color=black&color=brown

As you can see there are duplicated color arguments and it messes up the category listing. How can I remove same arguments if there is?

The original opencart's link builder function:

public function link($route, $args = '', $connection = 'NONSSL') {
    if ($connection ==  'NONSSL') {
        $url = $this->url;  
    } else {
        $url = $this->ssl;  
    }

    $url .= 'index.php?route=' . $route;

    if ($args) {
        $url .= str_replace('&', '&', '&' . ltrim($args, '&')); 
    }

    return $this->rewrite($url);
}

解决方案

There is not enough information to provide a correct answer, but I'll take a guess.

The problem seems to be with $args. It seems that you are taking $args from the URL and append to it your new color parameter.

If URL is index.php?route=product/category&path=59_63&size=57&color=black, then $args is path=59_63&size=57&color=black

You append to it color=brown and $args becomes path=59_63&size=57&color=black&color=brown.

If this is the case, you can do something like this:

parse_str($args,$url_params);
$url_params['color'] = 'brown'; //-- overwrites color=black with color=brown
$args = http_build_query($url_params);

Then pass $args to your link() function.

这篇关于PHP:如何删除URL中的重复的论点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆