Reputation: 51
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
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