I am trying to change all the links of a html with php preg_replace.
All the uris have the following form
http://example.com/page/58977?forum=60534#comment-60534
I want to change it to:
http://example.com/60534
which means removing everything after “page” and before “comment-“, including these two strings.
I tried the following, but it returns no changes:
$result = preg_replace("/^.page.*.comment-.$/", "", $html);
but it seems that my regex syntax is not correct, as it returns the html unchanged.
Could you please help me with this?
The
^is an anchor that only matches the start of the string, and$only matches at the end. In order to match you should not anchor the regular expression:Note that this could match things that are not URLs. You may want to be more specific as to what will be replaced, for example you might want to only replace links starting with either
http:orhttps:and that don’t contain whitespace.