简体   繁体   中英

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)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM