Return to Snippet

Revision: 11707
at February 15, 2009 21:57 by troynt


Initial Code
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);
}

Initial URL
http://www.addedbytes.com/php/querystring-functions/

Initial Description

                                

Initial Title
Adding and Removing Query Vars

Initial Tags
php

Initial Language
PHP