How do I remove a certain variable from a query string? Say I have a query string
$query_string = "first=val1&second=val2&third=val3";
function removevar($var, $query_string) {
return preg_replace("/(".$var."=[^&]*(&))/i","",$query_string);
}
echo removevar("first",$query_string); // ok
echo removevar("second",$query_string); // ok
echo removevar("third",$query_string); // doesn't change the string because third doesn't have a trailing &
How can this be fixed so that it removes variables from a query string in a robust way? Probably someone already has a function that does this along with special cases in more complex strings.
So I'd have to match either & or end of the string ($) but I don't know how to turn that into regex.
$varis "regex safe" by running it through preg_quote($var,'/') first.