Skip to content

Instantly share code, notes, and snippets.

@MahdiMajidzadeh
Last active August 6, 2022 20:35
Show Gist options
  • Save MahdiMajidzadeh/6d209db94f2bae6502c4530f9140e1d9 to your computer and use it in GitHub Desktop.
Save MahdiMajidzadeh/6d209db94f2bae6502c4530f9140e1d9 to your computer and use it in GitHub Desktop.
remove query params from url
<?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