Last active
August 6, 2022 20:35
-
-
Save MahdiMajidzadeh/6d209db94f2bae6502c4530f9140e1d9 to your computer and use it in GitHub Desktop.
remove query params from url
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
<?php | |
function clean_url($url, $params) | |
{ | |
$pieces = parse_url($url); | |
if (!$pieces['query']) { | |
return $url; | |
} | |
$query = []; | |
parse_str($pieces['query'], $query); | |
foreach ($params as $param) { | |
if (isset($query[$param])) { | |
unset($query[$param]); | |
} | |
} | |
if(count($query) > 0) { | |
$pieces['query'] = http_build_query($query); | |
} else { | |
unset($pieces['query']); | |
} | |
return ((isset($pieces['scheme'])) ? $pieces['scheme'] . '://' : '') | |
.((isset($pieces['host'])) ? $pieces['host'] : '') | |
.((isset($pieces['port'])) ? ':' . $pieces['port'] : '') | |
.((isset($pieces['path'])) ? $pieces['path'] : '') | |
.((isset($pieces['query'])) ? '?' . $pieces['query'] : ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment