regex - regular expression to remove ID=1234 from a string (PHP)
748
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
Answer
Solution:
You could use...
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...
Answer
Solution:
I'm guessing that
&
is not allowed as a character in theID
attribute. In that case, you can useor (possibly better, thanks to PaulP.R.O.):
This will remove
&ID=
(the second version would also remove?ID=
) plus any amount of characters that follow until the next&
or end of string. This approach makes sure that any following attributes will be left alone:will be changed into
Answer
Solution:
You can just use
(that is if the URL is of the form:
http://something.com?id1=1&id2=2
):