On my Github I created a gist with a function to do operations like this in a reusable way. Besides, I prefer not to use regular expression, as these usually cost more in terms of performance.
This function allows you to remove, replace or add parameters to a given url. If it already has Query String, it will be replaced or added.
Behold:
function url_replace_query($url, array $parameters)
{
$parts = parse_url($url) + [
'scheme' => 'http',
'query' => NULL,
'path' => NULL,
];
if (! isset($parts['host'])) return false;
parse_str($parts['query'], $query);
$parameters += $query;
$newQueryString = http_build_query($parameters);
return $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $newQueryString;
}
You can use it like this:
$url_sem_ordem = url_replace_query("http://www.meudominio.com.br/pagina.php?campo=teste&ordem=2&cor=azul", ['ordem' => null])
Upshot:
'http://www.meudominio.com.br/pagina.php?campo=teste&cor=azul'
possible duplicate of http://answall.com/a/123638/4793
– Daniel Omine