Okay.. so basically, say we have a link:
$url = \"http://www.site.com/index.php?sub=Mawson&state=QLD&cat=4&page=2&sort=z\";
function replaceQueryParams($url, $params)
{
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $oldParams);
if (empty($oldParams)) {
return rtrim($url, '?') . '?' . http_build_query($params);
}
$params = array_merge($oldParams, $params);
return preg_replace('#\?.*#', '?' . http_build_query($params), $url);
}
$url
examples:
$params
example:
[
'foo' => 'not-bar',
]
Note: it doesn't understand URLs with anchors (hashes) like http://example.com/page?foo=bar#section1