I'm stuck with an easy regex to match URLs in a content. The goal is to remove the folder from the links like "/folder/id/123" and to replace them with "id/123" so it's a short relative one in the same folder.
Actually I did
$pattern = "/\/?(\w+)\/id\/(\d)/i"
$replacement = "id/$2";
return preg_replace($pattern, $replacement, $text);
and it seems to work fine.
However, the last test that I'd like to to is to test that each url matched does NOT containt http://, if it's an external site which also use the same pattern /folder/id/123.
I tried /[^http://] or (?<!html)... and different things without success. Any help would be verys nice :-)
$pattern = "/(?<!http)\b\/?(\w+)\/id\/(\d)/i"; ???????
Thanks !
Here is some examples : Thanks you VERY MUCH for your help :-)
(these should be replaced, "same folder" => short relative path only)
<a href="/mysite_admin/id/414">label</a> ==> <a href="id/414">label</a>
<a href="/mYsITe_ADMIN/iD/29">label with UPPERCASE</a> ==> <a href="id/414">label with UPPERCASE</a>
(these should not be replaced, when there is http:// => external site, nothing to to)
<a href="http://mysite_admin/id/414">label</a> ==> <a href="http://mysite_admin/id/414">label</a>
<a href="http://www.google_admin.com">label</a> ==> <a href="http://www.google_admin.com">label</a>
<a href="http://anotherwebsite.com/id/32131">label</a> ==> <a href="http://anotherwebsite.com/id/32131">labelid/32131</a>
<a href="http://anotherwebsite_admin.com/id/32131">label</a> ==> <a href="http://anotherwebsite_admin.com/id/32131">label</a>