1

How do I remove certain parameter and its value from url?

<?php $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>

My urls can be pagename?page=2 or pagename?param=235235&page=2 or pagename?page=10&param=5431 or etc.

I need remove page=x from url.

0

3 Answers 3

1

A different approach using php's built-in url functions without regex. More elegant IMHO

function removeParamFromURL($param, $url) {
    $parsed_url = parse_url($url);
    parse_str($parsed_url["query"], $querystringarray);
    unset($querystringarray[$param]);
    $parsed_url["query"] = http_build_query($querystringarray);
    return $parsed_url["scheme"] . "://" . $parsed_url["host"] . $parsed_url["path"] . "?" . $parsed_url["query"];
}
$url = "http://example.com/aaa/bbb/pagename/page.php?param=235235&page=2";
print $finalurl = removeParamFromURL("page", $url);
Sign up to request clarification or add additional context in comments.

Comments

1

You have stated in your question that you wish to REMOVE page=xx from the URL.

There's Nothing like the smell of regex in the afternoon... So I had a stab at this and the following should do the trick.

I'm sure it can be done more smartly, but it gets you up and running.

<?php
/*
 * My urls can be
 * pagename?page=2 or
 * pagename?param=235235&page=2 or
 * pagename?page=10&param=5431 or etc.
 */
function remove_page_from_url($url){
    // For the case where ?page=xx&param
    if (preg_match('/\?page=[0-9]{0,}&/',$url)){
        $url =  preg_replace('/&/','?',$url,1); // Only replace the 1st one.
    }
    return preg_replace('/[\?|&]page=[0-9]{0,}/','',$url);
}

// The Testing during development

//Case 1: This works
$url = 'pagename?page=10';
echo remove_page_from_url($url);
echo '<br>';

//Case 2: This works
$url = 'pagename?param=235235&page=2';
echo remove_page_from_url($url);
echo '<br>';

//Case 3: This works
//This messes things up as we also have to change a & to a ?
$url = 'pagename?page=10&param=5431';
echo remove_page_from_url($url);
echo '<br>';

//Case 4: This Works
//This messes things up as we also have to change a & to a ? but only the first one.
$url = 'pagename?page=10&param=5431&something=2';
echo remove_page_from_url($url);
echo '<br>';

So you start from the simplest case and work your way up.

/*
The Results

pagename?page=10
=>   pagename

pagename?param=235235&page=2
=>   pagename?param=235235

pagename?page=10&param=5431
=>   pagename?param=5431

pagename?page=10&param=5431&something=2
=>   pagename?param=5431&something=2
*/

So it is the code inside function remove_page_from_url($url) that you are interested in.

Comments

1
Remove dynamic parameters from a URL

 <?php
function remove_url_parmeter($url,$parameter_key){
        if($parameter_key && $url){
            foreach($parameter_key as $val){
                 if (preg_match('/[\?|&]'.$val.'=[0-9]{0,}/',$url)){
                    $url =  preg_replace('/&/','?',$url,1);
                 }
                 $url = preg_replace('/[\?|&]'.$val.'=[0-9]{0,}/','',$url);
            }
        }else{
            $url = 'This is not work';
        }
    return $url;

}

$url = "http://example.com/pagename?page=5&param=5431&page1=10&somepage=3";
//$parameter_key =array('page','page1');
echo remove_url_parmeter($url,array('page','page1'));

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.