Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - add/update a parameter in a url [duplicate]

Tags:

regex

php

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.

like image 443
Sherif Buzz Avatar asked Nov 04 '10 19:11

Sherif Buzz


1 Answers

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;
 }
like image 152
Qwerty Avatar answered Sep 21 '22 07:09

Qwerty