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

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

by rob_au (Abbot)
on Feb 14, 2002 at 11:28 UTC ( #145424=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 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&&[@.]/&&s&.com.&_&&&print'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2016-10-25 09:48 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (315 votes). Check out past polls.