Skip to content Skip to sidebar Skip to footer

Detect Url And Add Achor-tags Around It

Recently I've developed a mailinglistsystem with PHP. Now, I'd like to make sure that if someone types a URL, the PHP-script detects it and adds the anchor-tags around it. So if I

Solution 1:

I use the following regex:

$link = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $link);

It may not be perfect, but it works in most cases.

Solution 2:

here is a little bit better version:

$link = preg_replace("/(http:\/\/[^\s]+\.+[a-zAZ]{2,6})/","<a href=\"$1\">$1</a>", $link);

This one checks if there is a dot in the link and a domain (like .com .org .nl)

Post a Comment for "Detect Url And Add Achor-tags Around It"