6

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.

6
  • SO rock but you are not. cause you have accepted a solution which will fail to replace many kinds of data. Commented Jul 22, 2010 at 12:56
  • @Col. Shrapnel: What kind of data would the solution not able to replace? The OP could then be interested of the case where the query parameters contains only numbers (as reported in the example query); if this the case, the proposed solution would work. Commented Jul 22, 2010 at 13:43
  • @kiamlaluno well if you take this site as "let's answer every lame question and forget it" it's o.k. But someone thinks of this site as an ultimate source of knowledge, where answers can be used more than once Commented Jul 22, 2010 at 15:13
  • @pondpad: See the comments to my answer (stackoverflow.com/questions/3308711#comments-3308749). Commented Jul 22, 2010 at 15:14
  • @Col. Shrapnel: The question is specific; I don't think someone can say the replies given here are generally valid. Generally speaking, the answer given to a question could not be valid in other cases; if in a case the solution is to use regular expressions, that doesn't mean regular expressions should always be used. Commented Jul 22, 2010 at 15:50

8 Answers 8

14

A solution using url parsing:

parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
Sign up to request clarification or add additional context in comments.

2 Comments

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);
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.
10

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

How about:

str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']);

3 Comments

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 stackoverflow.com/questions/3308711#comment-3428781) for an example.
Ah, yes. But you could fix that easy enough by wrapping $_GET['b'] in a decode thingy.
What if there is a ? before b=?
4

Try this:

$query_new = preg_replace('/(^|&)b=[^&]*/', '', $query);

10 Comments

What’s the reason for the down vote? And please don’t say “Oh, you’re using regular expressions! How bad!”
Using a regular expression would allow to replace the parameter whatever position it has in the query string. str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']) doesn't replace it if it's the first parameter in the query string.
@kiamlaluno: Exactly. That’s why I chose a regular expression search.
imagine we have $_GET['b'] = "direct replace sometimes fail";
@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.
|
4

you can use this function:

function Remove_QS_Key($url, $key) {
$url = preg_replace('/(?:&|(\?))'.$key.'=[^&]*(?(1)&|)?/i', "$1", $url);
return $url;
}

to remove any key you want, e.g.

echo Remove_QS_Key("http://domain.com/?a=b&ref=dusername&c=d&e=f&g=h", "ref");

result

http://www.domain.com/?a=b&c=d&e=f&g=h

Comments

2

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);

3 Comments

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'].
As far as I know, to create a reference you will need to do $getCopy =& $_GET...
@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.
1

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

4 Comments

Don’t forget to escape the output properly.
And what if a and c are not decimal values?
@Gumbo: What do you mean exactly?, are you asking or need to suggest something here?
@sAc: If you use urlencode instead of intval, it can be used on any values.
0

Pear already has a class(Net_URL2) that handles URL parsing/building:

Install via Composer: https://packagist.org/packages/pear/net_url2 Install as include: https://github.com/pear/Net_URL2/blob/master/Net/URL2.php

Example code:

$url = new Net_URL2('http://www.example.com/?one=1');
$url->setQueryVariable('two', 2);
echo $url; // http://www.example.com/?one=1&two=2

Comments

0

Here is a function to replace a query parameter: (like example.com?a=1&b=2 -> example.com?a=5&b=2)

function replace_qs_key($key, $value) {
  $current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
                 "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  $current_url_without_qs = strtok($current_url, '?');
  parse_str($_SERVER['QUERY_STRING'], $query_params);
  $query_params['page'] = $value;
  $_SERVER['QUERY_STRING'] = http_build_query($query_params);
  $new_url = $current_url_without_qs .'?'. $_SERVER['QUERY_STRING'];
  return $new_url;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.