
japhy
Enthusiast
Feb 3, 2000, 4:07 AM
Post #5 of 6
(434 views)
|
You can use a simple regular expression like the following <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> $string =~ s{ ( (?:ftp | https?) # match specific protocols :// # match the :// \S+ # match as many non-spaces as possible ) }{ my $url = $1; my ($junk) = $url =~ s!([^\w+&=/]*)$!!; <a href="$url">$url</a>$junk }xge; </pre><HR></BLOCKQUOTE> The left-hand side of the regex basically pulls out what we think is a URL. The right-hand side evaluate some Perl code to try and remove any non-URL junk at the end of the URL. For instance, if the string held "Visit my web site at http://www.foo.com/~me.", we wouldn't want to keep that . in the link, because it would foul things up. I'm not sure my approach is a spectacular one, but it should work well at not including things in the URL section that it shouldn't.
|