Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing URLs to Links

Status
Not open for further replies.

LucL

Programmer
Jan 23, 2006
117
US
Hi guys,

I have a string that contains all text but may also contain a URL, ex:

"This is a test message. If you wish visit the url below\n ok. that's it for now."

I want to be able to find all the URLs (they will always start with http:// and end with a space and replace them with <a href=URL>URL</a> where URL is the URL found

I know this should be possible with the perl pattern matching ~s functions but I have no idea how to approach it.

Thanks!
Luc L.
 
This should give you a start:

Code:
my $str = "This is a test message. If you wish visit the url below [URL unfurl="true"]http://www.something.com[/URL] ok.  You can also visit [URL unfurl="true"]http://www.google.com[/URL] ";
my @url = $str =~ /.+?(http:\/\/.*?)\s+/g;
print "<a href=\"$_\">$_</a>\n" foreach @url;
 
Thanks a lot raklet! That did the trick.
 
There's a lot of logic flaws with that one though. It's just enough to get you started. It won't pickup mailto: links and appears to only pick up links that start with http. You need to also look for www. urls, https, etc.
 
No, there is no flaw in the logic. It does exactly what the user asked for

they will always start with http:// and end with a space

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top