Thursday, 18 November 2010

PHP Add and Remove Querystring Variables

function add_querystring_var($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 remove_querystring_var($url, $key) {

$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');

$url = substr($url, 0, -1);

return ($url);

}

No comments:

Post a Comment