Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I replace a URL with a clickable hyperlink?

by tipthepizzaguy (Initiate)
on Feb 14, 2002 at 08:42 UTC ( [id://145404]=perlquestion: print w/replies, xml ) Need Help??

tipthepizzaguy has asked for the wisdom of the Perl Monks concerning the following question: (regular expressions)

Whenever there is "http://" followed by a number of characters until it reaches a space, new line, or end of file, it needs to replace it with HTML code so it becomes a clickable hyperlink. For example, it will replace http://mydomain.com with <a href="http://mydomain.com">http://mydomain.com</a>

Originally posted as a Categorized Question.

  • Comment on How do I replace a URL with a clickable hyperlink?

Replies are listed 'Best First'.
Re: How do I replace a URL with a clickable hyperlink?
by rob_au (Abbot) on Feb 14, 2002 at 11:28 UTC
    There is a module specifically built for this task - URI::Find. eg.

    #!/usr/bin/perl -Tw use URI; use URI::Find; use strict; my $text = "... long string with lots of URLs ..."; find_uris($text, sub { my ($find_uri, $orig_uri) = @_; my $uri = URI->new( $orig_uri ); $uri = $uri->canonical->as_string; return '<a href="' . $uri . '">' . $uri . '</a>'; }); print $text, "\n"; exit 0;

    Note that the CPAN documentation for URI::Find is out-of-date with the newest version (0.04) exporting only the one function, find_uris, which takes two arguments, the string to be searched and a function reference.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: How do I replace a URL with a clickable hyperlink?
by tachyon (Chancellor) on Feb 14, 2002 at 14:45 UTC
    $text = <<TEXT; Hello World http://www.world.com/index.htm http://foo.com http://bar.com TEXT ($links = $text) =~ s!(http://[^\s]+)!<a href="$1">$1</a>!gi; print $text,"\n\n",$links;
      This is wrong, of course, for unusual links that contain HTML-nasty characters, unless you presume that the input contains no quotes and is already HTML-safe.

      -- Randal L. Schwartz, Perl hacker

Re: How do I replace a URL with a clickable hyperlink?
by Abigail-II (Bishop) on Jul 14, 2003 at 15:17 UTC
    If you are interested in matching valid HTTP URI's, you could use Regexp::Common:
    use Regexp::Common; $text =~ s[($RE{URI}{HTTP})] [<a href = "$1">$1</a>]g;

    Abigail

Re: How do I replace a URL with a clickable hyperlink?
by tipthepizzaguy (Initiate) on Feb 15, 2002 at 08:32 UTC
    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;
      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.

Re: How do I replace a URL with a clickable hyperlink?
by tachyon (Chancellor) on Feb 14, 2002 at 14:46 UTC
    $text = <<TEXT; Hello World http://www.world.com/index.htm http://foo.com http://bar.com TEXT ($links = $text) =~ s!(http://[^\s]+)!<a href="$1">$1</a>!gi; print $text,"\n\n",$links;

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://145404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found