user3815283
user3815283

Reputation: 51

php remove QUERY_STRING variable

i have this function that should remove part of a query string:

if(!function_exists("remove_querystring_var")) {
    function remove_querystring_var($url, $key) {
        $url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
        $url = substr($url, 0, -1);
        return ($url);
    }
}

i have ahref links like:

<a href="link.php?<?php echo $_SERVER["QUERY_STRING"]; ?>">link</a>

but i need to be able to remove ?pagenum=X (X = a page number)

Upvotes: 0

Views: 216

Answers (1)

andrew
andrew

Reputation: 9583

you could just

unset($_GET['pagenum']);

and

<a href="link.php?<?= http_build_query($_GET) ?>">link</a>

Code would potentially look like:

<?php

// $_GET looks like: array('foo'=>'bar','pagenum'=>5,'abc'=>'xyz')

unset($_GET['pagenum']);

// now $_GET looks like: array('foo'=>'bar','abc'=>'xyz')

// so http_build_query($_GET) will look like: foo=bar&abc=xyz
?>


<a href="link.php?<?php echo http_build_query($_GET) ?>">link</a>

Upvotes: 3

Related Questions