Link to home
Start Free TrialLog in
Avatar of blokey
blokey

asked on

Detect a link

How can I detect a link in a string and surround that link with the <a href> tag?

For example:

$str = "visit my site at http://www.something.com or http://www.somethingelse.com";

I would then want that to become

$str = "visit my site at <a href=\"http://www.something.com\">http://www.something.com</a> or <a href=\"http://www.somethingelse.com\">http://www.somethingelse.com</a>";

Im thinking of using regular expressions, but I dont know how to go about it.

Blokey
Avatar of clockwatcher
clockwatcher

$str =~ s#(http://\S+)#<a href="$1">$1</a>#ig;

print $str;
ASKER CERTIFIED SOLUTION
Avatar of maxkir
maxkir
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The accepted answer only creates a link for the domain name not the entire URL, How can we fix that?