正则表达式从字符串中删除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);更多推荐
发布评论