Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Answer: How do I replace a URL with a clickable hyperlink?

by tipthepizzaguy (Initiate)
on Feb 15, 2002 at 08:32 UTC ( #145643=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How do I replace a URL with a clickable hyperlink? - Answer contributed by tipthepizzaguy

Thank you. I found a slightly shorter way based on tachyon's response. (Will open link in new window.)

$text = "Come visit http://mysite.com and see what it says."; $text =~ s!(http://[^\s]+)!<a href="$1" target="_new">$1</a>!gi; print $text;

Replies are listed 'Best First'.
Re: Answer: How do I replace a URL with a clickable hyperlink?
by htmanning (Monk) on Apr 06, 2005 at 00:45 UTC
    This code looks great but could someone tell me how to use this code if I don't have the http:// in the text? I have a string of text where it will simply say ABC.com. I want to turn that into ABC.com. Thanks. Tom

      "ABC.com" is not a URL -- it's a domain.

      Assuming you had first matched the domain (and had it in a variable named $domain) and you knew that all of the domains were using a host named 'www' for protocol 'http', you could use:

      my $link = "<a href='http://www.$domain'>$domain</a>";

      How you match domains is up to you... but I know that I get annoyed when my jabber client thinks that my discussions about perl scripts should be turned into links to websites in Poland.

        Well, here's what I ended up using: $text =~ s!(^\s+\.com)!$1!gi; This works but the only problem is that sometimes I'm grabbing ABC.com out of a block of text, that also includes an image. In that case, the image code is messed up because the above code looks for white space, then something, then .com, which also matches the html for the image. How can I have the image left along, and the above code applied to ABC.com? Thanks.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
erix googles
[erix]: pretty :)
[Your Mother]: Yep, LanX, here is the reverse operation: perl -CSD -MEncode -MMIME::Base64 -E 'say decode_utf8( decode_base64(" 4oGZ"))'
[LanX]: Thanks Many thanks YM... it was already supposed to be encoded, I'll see tomorrow
[Your Mother]: I am entirely skeptical the wall will happen… but this country does love its boongdoggles now and then. :P
[Your Mother]: Did I tell or show you? There is a leucistic Turkey Vulture around here all the time. I have a couple decent pictures.
[Your Mother]: Pure white (well, dirty white :P)
[erix]: as a kid I had a painted bunting -- beautiful
[Your Mother]: I would love to see one. It would be a lifer.
[Your Mother]: Wait… maybe I did see one last year…

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2017-01-20 01:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (173 votes). Check out past polls.