【问题标题】:php - add/update a parameter in a url [duplicate]php - 在url中添加/更新参数[重复]
【发布时间】:2011-05-05 06:32:32
【问题描述】:

可能重复:
Change single variable value in querystring

我发现这个函数可以将参数添加或更新到给定的 url,它在需要添加参数时起作用,但如果参数存在,它不会替换它 - 抱歉,我对正则表达式不太了解,任何人都可以请看一下:

function addURLParameter ($url, $paramName, $paramValue) {
    // first check whether the parameter is already
    // defined in the URL so that we can just update
    // the value if that's the case.

    if (preg_match('/[?&]('.$paramName.')=[^&]*/', $url)) {

        // parameter is already defined in the URL, so
        // replace the parameter value, rather than
        // append it to the end.
        $url = preg_replace('/([?&]'.$paramName.')=[^&]*/', '$1='.$paramValue, $url) ;
    } else {
        // can simply append to the end of the URL, once
        // we know whether this is the only parameter in
        // there or not.
        $url .= strpos($url, '?') ? '&' : '?';
        $url .= $paramName . '=' . $paramValue;
    }
    return $url ;
}

这是一个不起作用的例子:

http://www.mysite.com/showprofile.php?id=110&l=arabic

如果我用 l=english 调用 addURLParameter,我会得到

http://www.mysite.com/showprofile.php?id=110&l=arabic&l=english

提前致谢。

【问题讨论】:

  • 该功能对我来说看起来不错。你能举一个你试图替换的参数的例子,你得到了什么结果?
  • @Bruce Alderman 示例已添加,谢谢。
  • 我不确定出了什么问题;我在这里进行了几次测试,但无法重现该错误。无论如何,如果您不了解正则表达式,它们将不是最好的解决方案。当您需要维护代码时会发生什么?

标签: php regex


【解决方案1】:

为什么不使用标准的 PHP 函数来处理 URL?

function addURLParameter ($url, $paramName, $paramValue) {
     $url_data = parse_url($url);
     $params = array();
     parse_str($url_data['query'], $params);
     $params[$paramName] = $paramValue;   
     $params_str = http_build_query($params);
     return http_build_url($url, array('query' => $params_str));
}

抱歉没有注意到 http_build_url 是 PECL :-) 让我们滚动我们自己的build_url 函数。

function addURLParameter($url, $paramName, $paramValue) {
     $url_data = parse_url($url);
     if(!isset($url_data["query"]))
         $url_data["query"]="";

     $params = array();
     parse_str($url_data['query'], $params);
     $params[$paramName] = $paramValue;   
     $url_data['query'] = http_build_query($params);
     return build_url($url_data);
}


 function build_url($url_data) {
     $url="";
     if(isset($url_data['host']))
     {
         $url .= $url_data['scheme'] . '://';
         if (isset($url_data['user'])) {
             $url .= $url_data['user'];
                 if (isset($url_data['pass'])) {
                     $url .= ':' . $url_data['pass'];
                 }
             $url .= '@';
         }
         $url .= $url_data['host'];
         if (isset($url_data['port'])) {
             $url .= ':' . $url_data['port'];
         }
     }
     $url .= $url_data['path'];
     if (isset($url_data['query'])) {
         $url .= '?' . $url_data['query'];
     }
     if (isset($url_data['fragment'])) {
         $url .= '#' . $url_data['fragment'];
     }
     return $url;
 }

【讨论】:

  • 谢谢,但我在一个没有 PECL 的共享主机上
  • @Sherif 他更新了他的答案。 +1,这很好(虽然它应该在原始问题中,而不是在副本中,为了后代)
猜你喜欢
  • 2023-03-25
  • 2012-10-15
  • 2015-11-25
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多