Created
December 3, 2010 14:22
-
-
Save DaveChild/727006 to your computer and use it in GitHub Desktop.
Add and remove querystring variables from URLs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Add and remove querystring variables from URLs. | |
*/ | |
function addQuerystringVar($url, $key, $value) { | |
$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); | |
$url = substr($url, 0, -1); | |
if (strpos($url, '?') === false) { | |
return ($url . '?' . $key . '=' . $value); | |
} else { | |
return ($url . '&' . $key . '=' . $value); | |
} | |
} | |
function removeQuerystringVar($url, $key) { | |
$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); | |
$url = substr($url, 0, -1); | |
return ($url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment