0

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)

5
  • 2
    parse_str() + http_build_query() Commented Dec 8, 2014 at 22:28
  • 2
    So does your remove_querystring_var function work? Not work? What's the question here? Commented Dec 8, 2014 at 22:29
  • Btw, if(!function_exists("remove_querystring_var")) { looks like fixing consequences not the roots. Commented Dec 8, 2014 at 22:29
  • Does $url = preg_replace('?pagenum=X', ''); not work or is that not what you needed to do? Commented Dec 8, 2014 at 22:33
  • locks very XY to me Commented Dec 8, 2014 at 22:33

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

how do i use unset in a href link ?
you don't, you unset the GET variable before building a new query string, ill edit the answer to make it more clear

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.