PHP:删除除一个查询字符串变量之外的所有变量

发布于 2025-01-06 11:55:06 字数 374 浏览 3 评论 0原文

假设我有以下内容,即当前 url:

http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom

How will I remove allparameters except S using PHP?换句话说,只需离开: http://myurl.com/locations/?s=bricks 其他参数将始终是 Style、Type、Feature 和 Area,其中部分或全部可能出席。

谢谢!

Suppose I have the following, which is the current url:

http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom

How would I remove all parameters except S using PHP? In other words, just leaving: http://myurl.com/locations/?s=bricks The other parameters will always be Style, Type, Feature, and Area, and some or all of them may be present.

Thanks!

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

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

发布评论

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

评论(5

醉生梦死 2025-01-13 11:55:06
if ( isset($_GET['s']) && count($_GET) > 1 ) {
    header('Location: ?s='. rawurlencode($_GET['s']));
    exit;
}

编辑是为了安抚乔恩。

if ( isset($_GET['s']) && count($_GET) > 1 ) {
    header('Location: ?s='. rawurlencode($_GET['s']));
    exit;
}

Edited to somewhat appease Jon.

樱娆 2025-01-13 11:55:06

错误检查的方式不多,但如果保证 s 存在,这将起作用:

list($base, $query) = explode('?', $url);
parse_str($query, $vars);
$result = $base.'?s='.rawurlencode($vars['s']);

无论 s 出现在查询字符串中的哪个位置,它都将起作用。我正在使用 explode 进行最小的初始“解析”步骤,但如果感觉不合适,您可以随时使用大枪并使用 parse_url 相反,代价是更加冗长。

Not much in the way of error checking, but if s is guaranteed to exist this will work:

list($base, $query) = explode('?', $url);
parse_str($query, $vars);
$result = $base.'?s='.rawurlencode($vars['s']);

It will also work no matter in what position s appears in the query string. I 'm doing a minimal initial "parsing" step with explode, but if that doesn't feel right you can always bring in the big guns and go with parse_url instead at the expense of being much more verbose.

走野 2025-01-13 11:55:06

您可以通过几种不同的方式来实现这一点。

我将向您展示我在处理 url 时遇到的最佳标准:

$originUrl = parse_url('http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom'); 
parse_str($originUrl['query'], $queryString);
echo $newUrl = $originUrl['scheme'].'://'.$originUrl['host'].$originUrl['path'].'?s='.$queryString['s'];  

祝您好运!

You can achieve this in several different manners.

I will show you the best standard I ever faced to work with urls:

$originUrl = parse_url('http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom'); 
parse_str($originUrl['query'], $queryString);
echo $newUrl = $originUrl['scheme'].'://'.$originUrl['host'].$originUrl['path'].'?s='.$queryString['s'];  

Good Luck!

故人爱我别走 2025-01-13 11:55:06
parse_str(parse_url($url, PHP_URL_QUERY), $query);
$newurl = 'http://myurl.com/locations/?s=' . $query['s'];
parse_str(parse_url($url, PHP_URL_QUERY), $query);
$newurl = 'http://myurl.com/locations/?s=' . $query['s'];
反话 2025-01-13 11:55:06

如果您知道 s 始终是第一个参数,那么您可以简单地执行以下操作:

$url = "http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom";
$split = explode('&',$url);
echo $split[0];

但是,如果 s 在参数中的位置未知,那么您可以这样做:

$url = "http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom";
$split = explode('?',$url);
parse_str($split[1],$params);
echo $split[0].'?s='.$params['s'];

If you know that s will always be the first parameter, then you can simply do:

$url = "http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom";
$split = explode('&',$url);
echo $split[0];

However, if the position of s in the parameters is unkown, then you can do this:

$url = "http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom";
$split = explode('?',$url);
parse_str($split[1],$params);
echo $split[0].'?s='.$params['s'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文