从查询字符串中删除某个变量

发布于 2024-10-29 23:56:10 字数 618 浏览 3 评论 0原文

如何从查询字符串中删除某个变量?假设我有一个查询字符串,

$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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷路的信 2024-11-05 23:56:10
$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

这应该可以解决问题。

$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.

沙与沫 2024-11-05 23:56:10

为此,您不一定需要正则表达式,因为 PHP 确实具有可以解析和构建查询字符串的函数 (parse_strhttp_build_query 分别):

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

请注意,您需要解码使用这些函数之前先参考 HTML 字符。

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.

柏拉图鍀咏恒 2024-11-05 23:56:10

您可能会更幸运地使用:

  1. html_entity_decode 来获得“正常”查询字符串。
  2. parse_str 将查询字符串放入数组中。
  3. 取消设置该数组中所需的键。
  4. 使用 http_build_query 重建字符串。
  5. 对其调用 htmlspecialchars 以将 & 返回到 &

不如正则表达式路由简洁,但更不容易出错。

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.

橪书 2024-11-05 23:56:10
<?php
$query_string = "first=val1&second=val2&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, '', '&');
}

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

通常,对于像这样涉及查询字符串的代码,最好始终使用内置的 PHP 函数,而不是正则表达式语法/公式。这就是我所做的,代码的主要部分包括以下 PHP 内置函数:-

希望有帮助。

<?php
$query_string = "first=val1&second=val2&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, '', '&');
}

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.

明月夜 2024-11-05 23:56:10
function removevar($var, $query_string) {
    $query_string = preg_replace("#$var=([^&]+)#is", "", $query_string);
    return trim($query_string, "&");
}
function removevar($var, $query_string) {
    $query_string = preg_replace("#$var=([^&]+)#is", "", $query_string);
    return trim($query_string, "&");
}
轻拂→两袖风尘 2024-11-05 23:56:10

有些函数可以直接处理查询字符串。

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文