How To Remove Query String From URL in PHP?

Today I needed to remove the query string or parameter from the URL. I found this quick function which does exactly what i needed i.e remove query string from URL.
function remove_qs_key($url, $key) {
 return preg_replace(‘/(?:&|(\?))’ . $key . ‘=[^&]*(?(1)&|)?/i’, “$1″, $url);
}
For example, if your URL was this:
  $url = http://example.com/test/index?id=55&template=view&format=pdf
  $key = 'format';
The above function would return:
  echo remove_qs_key($url, $key);
 //output: http://example.com/test/index?id=55&template=view
All I needed to do was provide the URL and the key to remove -- so easy and simple :)
Thanks to http://davidwalsh.name/php-remove-variable

Comments

Popular Posts