正则表达式从字符串中删除ID = 1234(PHP)(regular expression to remove ID=1234 from a string (PHP))

编程入门 行业动态 更新时间:2024-10-06 21:29:11
正则表达式从字符串中删除ID = 1234(PHP)(regular expression to remove ID=1234 from a string (PHP))

我正在尝试创建一个正则表达式来执行以下操作(在preg_replace中)

$str = 'http://www.site.com&ID=1620'; $str = 'http://www.site.com';

如何编写preg_replace来简单地从字符串中删除&ID = 1620(考虑到ID可能是变量字符串长度

提前致谢

I am trying to create a regular expression to do the following (within a preg_replace)

$str = 'http://www.site.com&ID=1620'; $str = 'http://www.site.com';

How would I write a preg_replace to simply remove the &ID=1620 from the string (taking into account the ID could be variable string length

thanks in advance

最满意答案

你可以用......

$str = preg_replace('/[?&;]ID=\d+/', '', $str);

我假设这是一个普通的URL,因此[?&;] 。 如果是这样的话, &应该是? 。

如果它是更大的GET参数列表的一部分,你可能最好使用...

parse_str($str, $params); unset($params['ID']); $str = http_build_query($params);

You could use...

$str = preg_replace('/[?&;]ID=\d+/', '', $str);

I'm assuming this is meant to be a normal URL, hence the [?&;]. If that's the case, the & should be a ?.

If it's part of a larger list of GET params, you are probably better off using...

parse_str($str, $params); unset($params['ID']); $str = http_build_query($params);

更多推荐

本文发布于:2023-07-09 10:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   ID   PHP   expression

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!