PHP-在URL中添加/更新参数 [英] php - add/update a parameter in a url

查看:726
本文介绍了PHP-在URL中添加/更新参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
更改查询字符串中的单个变量值

Possible Duplicate:
Change single variable value in querystring

我发现此函数可以将参数添加或更新到给定的url,当需要添加参数时它可以工作,但是如果该参数存在,则不能替换它-抱歉,我对regex不太了解请看看:

i found this function to add or update a parameter to a given url, it works when the parameter needs to be added, but if the parameter exists it doesn't replace it - sorry i do not know much about regex can anybody please have a look :

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,我会得到

if i call addURLParameter with l=english, i get

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

提前谢谢.

推荐答案

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

Why not to use standard PHP functions for working with URLs?

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函数.

Sorry didn't notice that http_build_url is PECL :-) Let's roll our own build_url function then.

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;
 }

这篇关于PHP-在URL中添加/更新参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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