8

Possible Duplicate:
Change single variable value in querystring

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

here's an example of what doesn't work :

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

if i call addURLParameter with l=english, i get

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

thanks in advance.

3
  • The function looks OK to me. Can you give an example of a parameter you are trying to replace, and what result you are getting? Commented Nov 4, 2010 at 20:13
  • @Bruce Alderman example added thanks. Commented Nov 4, 2010 at 20:49
  • I'm not sure what's wrong; I ran a couple tests here and was not able to reproduce the error. Regardless, if you don't understand regular expressions, they won't be the best solution. What would happen when you needed to maintain the code? Commented Nov 5, 2010 at 5:07

1 Answer 1

19

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

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;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but I'm on a shared host that doesn't have PECL
@Sherif he updated his answer. +1, this is good (although it should be in the original question and not here in the duplicate, for the sake of future generations)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.