I am trying get all html links within a string and replace them using preg_replace to another link (for link tracking etc)
It works fine on links like http://www.facebook.com but not those that do not have a ‘www’ within the string.
So the first URL would be fine, but the latter wouldn’t work – can anyone suggest how I alter my expression to allow BOTH links like this to work.
http://www.twitter.com
Posts by myusername
$message = preg_replace("/<a([^>]+)href=\"http\:\/\/([a-zA-Z0-9\-]+\.[a-zA-Z0-9]+\.[a-zA-Z]{2,3}(\/*)?)/", "<a$1href=\"http://www.site.com/system/link_tracker.php?URL=$2&ID={$ID}\"", $message);
This is by no means a URL regex. It might work for two or three cases, but you ignore the existence of:
Users will especially not like the latter, because when they deeplink to some site, this link is rendered invalid using your regex. Use a common way, for example
DOMDocument.replaceChild()to alter links in an HTML document (which I assume you use, since you’re capturing URL’s in<a>tags).As said in this answer, that can be done with something like this, not tested: