Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Linkifying URLs in plain text

by jeffa (Bishop)
on Nov 21, 2008 at 16:28 UTC ( [id://725173]=note: print w/replies, xml ) Need Help??


in reply to Linkifying URLs in plain text

This seems to work. :)

use strict; use warnings; use Data::Dumper; use URI::Find; my $data = do {local $/;<DATA>}; my $finder = URI::Find->new( sub { my($uri, $orig_uri) = @_; return qq|<a href="$uri">$orig_uri</a>|; } ); $finder->find( \$data ); print $data; __DATA__ Blah blah blah Web: http://www.example.com info.example.com Web: http://info.example.com/ And another Web site: http://another.example.com/ (doesn't always work) Nice blog here: http://blog.example.com/niceblog/

UPDATE: change the inner sub to this to get exactly what you want:

sub { my $u = URI->new( $_[0] ); return sprintf( '<a href="%s">%s%s</a>', $u, $u->host, $u->path ); }

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found