in reply to detecting URLs and turning them into links
(using lookbehind assertions, see perldoc perlre). This, however, will not replace the URL that starts in the very first character in $message (since it's not following no whitespace). We can replace that separately:$message =~ s#(?<=\s)(http://\S+)#<a href="$1">$1</a>#g;
URLs are, however, often followed by a punctuation sign, which unfortunately will be included in our link (as in "http://google.com, for example…"). To remove this effect, make URLs end with a letter:$message =~ s#^(http://\S+)#<a href="$1">$1</a>#; $message =~ s#(?<=\s)(http://\S+)#<a href="$1">$1</a>#g;
I could go on talking about funny characters and special cases, but I'll just stop here and suggest you use Regexp::Common:$message =~ s#^(http://\S+[a-z])#<a href="$1">$1</a>#; $message =~ s#(?<=\s)(http://\S+[a-z])#<a href="$1">$1</a>#g;
use Regexp::Common qw( URI ); $message =~ s#^($RE{URI}{HTTP})#<a href="$1">$1</a>#; $message =~ s#(?<=\s)($RE{URI}{HTTP})#<a href="$1">$1</a>#g;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: detecting URLs and turning them into links
by polettix (Vicar) on Aug 24, 2007 at 00:39 UTC | |
by moritz (Cardinal) on Aug 24, 2007 at 07:56 UTC | |
by polettix (Vicar) on Aug 24, 2007 at 08:50 UTC | |
by moritz (Cardinal) on Aug 24, 2007 at 09:15 UTC |