PHP function to remove a key from a query string

I have a query string, for example:

n1 = v1 & n2 = v2 & n3 = v3

etc.

I just want the php function to accept the query string and name (key) and remove a pair of name values ​​from the query string.

Each example I found uses regular expressions and assumes that the value will exist and that it will not be empty, but in my case (and perhaps, in the general case, I would like to know) this would also be a valid query:

n1 = v1 & n2 = & n3

I do not know in advance how many keys will be.

I am convinced that regular expressions are monsters that eat time. In the end, all matter in the universe will be in regular expression.

+7
source share
2 answers
parse_str('n1=v1&n2=&n3', $gets); unset($gets['n3']); echo http_build_query($gets); 

NOTE. unset($gets['n3']); - This is an example to show.

+11
source

  function stripkey ($ input, $ key) {

$parts= explode("&", $input); foreach($parts as $k=>$part) { if(strpos($part, "=") !== false) { $parts2= explode("=", $part); if($key == $parts2[0]){ unset($parts[$k]); } } } return join("&", $parts); 

}

0
source

All Articles