Created
August 21, 2018 17:30
-
-
Save codetycon/b5e3817ae098dc1e6e2680bbf1705899 to your computer and use it in GitHub Desktop.
How to remove query parameter from url by parameter name in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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