views:

81

answers:

5

I use $_SERVER['QUERY_STRING'] to get the query sting.

A example would be a=123&b=456&c=789

How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric.

Any ideas appreciated, thanks.

+1  A: 

You simply make a variable using $_GET and exclude b query string in build process:

$query_string_new = 'a=' . urlencode($_GET['a']) . '&c=' . urlencode($_GET['c']);

The $query_string_new should now contain a=123&c=789

Sarfraz
Don’t forget to escape the output properly.
Gumbo
@Gumbo: Sure updated, thanks.
Sarfraz
And what if *a* and *c* are not decimal values?
Gumbo
@Gumbo: What do you mean exactly?, are you asking or need to suggest something here?
Sarfraz
@sAc: If you use `urlencode` instead of `intval`, it can be used on any values.
Gumbo
@Gumbo: O true !, forgot it was dealing with url, thanks :)
Sarfraz
A: 

Try this:

$query_new = preg_replace('/(^|&)b=[^&]*/', '', $query);
Gumbo
What’s the reason for the down vote? And please don’t say “Oh, you’re using regular expressions! How bad!”
Gumbo
kiamlaluno
@kiamlaluno: Exactly. That’s why I chose a regular expression search.
Gumbo
imagine we have `$_GET['b'] = "direct replace sometimes fail";`
Col. Shrapnel
@Gumbo: I take that sometimes the code shown from who creates the question is taken too literally; the fact the parameter `b` is shown in second position doesn't mean it should be always in second position. The OP is asking to remove that parameter, but he doesn't say it is always the second parameter, nor is it never the first parameter. That's why I think your answer is valid as other answers. If then the OP prefers an array instead of a string, that is something the OP should report.
kiamlaluno
@Col. Shrapnel: `/[^ I fail to see what your objection is.
kiamlaluno
@kiamlaluno: I think Col. Shrapnel’s comment was to address the flaw the accepted answer has that is requires the value to be unencoded. In opposite to that if `$_GET['b']` equals `direct replace sometimes fail` the value in the query string can either be `direct+replace+sometimes+fail`, `direct%20replace%20sometimes%20fail` or even a combination of `+` and `%20` but it’s never `direct replace sometimes fail`.
Gumbo
@Gumbo: As you proposed to use `preg_replace()`, I take that the variable `$query` you are referring to is a copy of `$_SERVER['QUERY_STRING']`; maybe that should be made clear in your answer. The question doesn't report how the obtained result is used; as the question is only how to remove a query parameter from the query string, your answer replies to what the OP asked. If then he needs to re-encode the string before to use it, that is a different question.
kiamlaluno
@kiamlaluno: `$query` can contain any valid query string. This solution does not depend on an actually requested query string that is parsed by PHP in advance to obtain an array of its parameters (i.e. `$_GET`) like other suggestions do (just like your own). And it does not require any specific parameter presence or parameter order neither.
Gumbo
@Gumbo: I didn't say your answer was not correct. I am simply saying that the comment made from Col. Shrapnel doesn't make sense for your answer.
kiamlaluno
A: 

The value is going to be $_GET['b'].

How about:

str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']);
Rimian
Im liking this one. Thank you.
pondpad
This solution fails if the value contains characters that need to be encoded. See Col. Shrapnel’s comment in the comments to my answer (see http://stackoverflow.com/questions/3308711#comment-3428781) for an example.
Gumbo
Ah, yes. But you could fix that easy enough by wrapping $_GET['b'] in a decode thingy.
Rimian
+4  A: 

A solution using url parsing:

parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
SirDarius
Something like this, definitely. You may be able to use the existing `$_GET` rather than doing the initial `parse_str()`: `unset($_GET['b']); $qs = http_build_query($_GET);`
Adam Backstrom
The difference is that my answer affects only the `$_SERVER['QUERY_STRING']` variable, and leaves $_GET untouched, in case it would still be needed for some reason.
SirDarius
+1  A: 

All the answers look good, but it will be more flexible if you do:

// Make a copy of $_GET to keep the original data
$getCopy = $_GET;
unset($getCopy['b']); // or whatever var you want to take out

// This is your cleaned array
var_dump($getCopy);

// If you need the URL-encoded string, just use http_build_query()
$encodedString = http_build_query($getCopy);
illarra
In PHP 5.3 `$getCopy = $_GET` doesn't create a copy, but it creates a reference to `$_GET`. I had the same problem in my code, where I wanted to preserve the value of `$_GET` that was used for another function; I used `$query = $_GET; unset($query['q'];`, and the result has been that also the other function (which was accessing `$_GET` directly) was not able to retrieve `$_GET['q']`.
kiamlaluno
illarra
@ilarra: That's correct; only objects are passed by reference, in PHP 5.3. I cannot then generalize the code I reported, even though replacing `$query = $_GET; unset($query['q'];` with the loop resolved the issue present in my code.
kiamlaluno