The page: /index.php?page=6&test=1&test2=2
The code below strip's page=6 off of this so I can add our new page to the url and add the $url_without_page_var back to our link again:
$_SERVER['argv'][0]
// Displays: page=6&test=1&test2=2
And
$url_without_page_var=preg_replace('/page=(\d+)/i','',$_SERVER['argv'][0]);
// Displays: &test=1&test2=2
Ok so that code lets me take the page=6 off of page=6&test=1&test2=2 I can then change the page number and add this all back together.
My problem is often the page in the URL will not always be in the same position, it may not be the 1st or last items And this is what will happen when, see how the URL is incorrect now;
/page.php?&**test=1&test2=2 **ERROR-HERE-ALSO page=9
The $_GET variable provides an array of all the variables set through the URL. $_REQUEST is an array of all the GET and POST variables set. Here's the fish:
$url_string = "index.php?";
foreach( $_GET as $k => $v ){
if( $k != "key_you_dont_want"){ // <-- the key you don't want, ie "page"
if( $url_string != "index.php?" )
$url_string .= "&"; // Prepend ampersands nicely
$url_string .= $k . "=" . $v;
}
}
Regex is a bit overkill for this problem. Hope it makes sense. I've been writing Python and Javascript for the past few weeks. PHP feels like a step backwards.
EDIT: This code makes me happier. I actually tested it instead of blindly typing and crossing fingers.
unset( $_GET["page"] );
$c = count($_GET);
$amp = "";
$url_string = "test.php";
if( $c > 0 ) {
$url_string .= "?";
foreach( $_GET as $k => $v ){
$url_string .= $amp . $k . "=" . $v;
$amp = "&";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With