Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How do I replace a URL with a clickable hyperlink?

( #145404=categorized question: print w/ replies, xml ) Need Help??
Contributed by tipthepizzaguy on Feb 14, 2002 at 08:42 UTC
Q&A  > regular expressions


Description:

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>

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

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'

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

$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;
Answer: How do I replace a URL with a clickable hyperlink?
contributed by Abigail-II

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

Answer: How do I replace a URL with a clickable hyperlink?
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;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2014-07-26 20:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (179 votes), past polls