Skip to content

Instantly share code, notes, and snippets.

@codetycon
Created August 21, 2018 17:30
Show Gist options
  • Save codetycon/b5e3817ae098dc1e6e2680bbf1705899 to your computer and use it in GitHub Desktop.
Save codetycon/b5e3817ae098dc1e6e2680bbf1705899 to your computer and use it in GitHub Desktop.
How to remove query parameter from url by parameter name in PHP
/* Function to remove query parameter by name*/
function removeQueryParam($url, $key)
{
return preg_replace('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', "$1", $url);
}
//How to use
$url = "https://gist.github.com/?test=false";
$key = "test";
$newURL = removeQueryParam($url,$key);
//Output will be ==> https://gist.github.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment