简体   繁体   English

忽略带有正则表达式的URI段中的查询字符串?

[英]Ignore query string in URI segments with regular expression?

I have the following URI: 我有以下URI:

/host/segment1[?parm1=value1&param2=value2]/segment2/segment3[?parm1=value1&param2=value2]

As you can see I would like to ommit the content inside the square brackets just like if it were: 如您所见,我想省略方括号内的内容,就像是这样:

/host/segment1/segment2/segment3

EDIT:The square brackets are just examples of the content I want to ommit, they do not actully appear in the segments. 编辑:方括号只是我要忽略的内容的示例,它们没有在段中出现。 A real examle would be: 一个真正的例子是:

/host/segment1?parm1=value1&param2=value2/segment2/segment3?parm1=value1&param2=value2

Is it possible to achieve this with a regular expression?? 是否可以用正则表达式实现?

You do that with this: 您可以这样做:

$result = preg_replace('~\?[^/]++~', '', $uri);

Details: 细节:

\?       question mark
[^/]++  all characters that are not a slash one or more times

~        used to delimite the regular expression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM