Remove a Query String Key=>Value using PHP

By | 1st May 2011

I needed to find a PHP function that would allow me to remove keys (and their values) easily and reliably.

/* the function */
function remove_querystring_var($url, $key) {
  $url = preg_replace(‘/(.*)(?|&)’ . $key . ‘=[^&]+?(&)(.*)/i’, ‘$1$2$4’, $url . ‘&’);
  $url = substr($url, 0, -1);

  return $url;
}

Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *