PHP:删除除一个查询字符串变量之外的所有变量
假设我有以下内容,即当前 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑是为了安抚乔恩。
Edited to somewhat appease Jon.
错误检查的方式不多,但如果保证
s
存在,这将起作用:无论
s
出现在查询字符串中的哪个位置,它都将起作用。我正在使用explode
进行最小的初始“解析”步骤,但如果感觉不合适,您可以随时使用大枪并使用parse_url
相反,代价是更加冗长。Not much in the way of error checking, but if
s
is guaranteed to exist this will work:It will also work no matter in what position
s
appears in the query string. I 'm doing a minimal initial "parsing" step withexplode
, but if that doesn't feel right you can always bring in the big guns and go withparse_url
instead at the expense of being much more verbose.您可以通过几种不同的方式来实现这一点。
我将向您展示我在处理 url 时遇到的最佳标准:
祝您好运!
You can achieve this in several different manners.
I will show you the best standard I ever faced to work with urls:
Good Luck!
如果您知道
s
始终是第一个参数,那么您可以简单地执行以下操作:但是,如果
s
在参数中的位置未知,那么您可以这样做:If you know that
s
will always be the first parameter, then you can simply do:However, if the position of
s
in the parameters is unkown, then you can do this: