2

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.

3
  • 1
    How about using parse_str and unset? Commented Apr 3, 2011 at 19:59
  • why you want to do this, you already have the $_GET array, you can just take the parameters you need and if you really want to remove one, just rebuilt the string using the array without this one... Commented Apr 3, 2011 at 20:00
  • 1
    To make it more robust, I would ensure that the $var is "regex safe" by running it through preg_quote($var,'/') first. Commented Apr 3, 2011 at 20:10

6 Answers 6

3
$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); // ok

This should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

3

You don’t necessarily need regular expressions for this as PHP does have functions that can parse and build query strings (parse_str and http_build_query respectively):

function removevar($var, $query_string) {
    parse_str($query_string, $args);
    unset($args[$var]);
    return http_build_query($args);
}

Note that you need to decode the HTML character references before using these functions.

1 Comment

Thanks to everyone who suggested the parse_str and http_build_query. Seems like PHP has functions for everything. I will be using that in the future but for reasons I can't go into I needed the regex for this in any case.
2

You will probably have more luck using:

  1. html_entity_decode to get the 'normal' query-string.
  2. parse_str to get the query string into an array.
  3. unset the desired key in that array.
  4. Use http_build_query to rebuild the string.
  5. Call htmlspecialchars on it to get the & back to &.

Less concise than the regex route, but a lot less error-prone.

Comments

2
<?php
$query_string = "first=val1&amp;second=val2&amp;third=val3";
parse_str($query_string, $output);

function removevar($var, $output_array) {
  if (in_array($var, $output_array)) {
    unset($output_array[$var]);
  }

  return http_build_query($output_array, '', '&amp;');
}

echo removevar("first", $output);
echo removevar("third", $output);
?>

Normally for codes like this, which involves a query string, it is always best to go for the in-built PHP functions rather than for the regex syntax / formula. That is what I've done, and the main parts of the code include the following PHP in-built functions:-

Hope it helps.

Comments

1
function removevar($var, $query_string) {
    $query_string = preg_replace("#$var=([^&]+)#is", "", $query_string);
    return trim($query_string, "&amp;");
}

Comments

1

There are functions, that can handle query strings directly.

function removevar ($var, $query_string) {
  $array = array();
  parse_str(html_entity_decode($query_string), $array);
  unset($array[$var]);
  return html_entities(http_build_query($array);
}

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.