Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Clickable link conversion regex?

Reply
Thread Tools

Clickable link conversion regex?

 
 
Tuxedo
Guest
Posts: n/a
 
      11-30-2012
Can anyone suggest a solution to enclose bare urls with href tags?

open(my $fh, 'urls.txt') or die $!;

while (my $line = <$fh>) {
$line =~ s[...] # match http or https instances
[...]s; # replace with enclosing hrefs
print $line;
}

The input format may be one or more URLs p/line.

Each scheme begins with either http:// or https:// but not necessarily as a
first string on a line.

Each URL ends with either the end of a line or a whitespace.

The input file would look like for example:

---------- urls.txt -------

http://www.example.com/hello
http://www.example.com/

bla https://www.example.com/a_page.htm plus a string not part of the URL

-----------

If an http or https string already has a preceding occurrence of a closing
html tag ">", such as:
<a href=http://bla.com>http://bla.com</a>
.... then it should be excluded with no replacement.

Two conditions exist in the input file:

The 'http' or 'https' bit will always begin at the first character on a new
line or have a preceding whitespace immediately before itself, like:

http://someurl.com line w/ whitespace before
http://someother.com
hello http://bla.com also w/ a whitespace before

The match and replace output on the above three lines would then be:

<a href=http://someurl.com>http://someurl.com</a> line w/ whitespace before
<a href=http://someother.com>http://someother.com</a>
hello <a href=http://bla.com>http://bla.com</a> also w/ a whitespace before

In case something may written as http://bla, which as in this sentence
isn't a link, it would inadvertently end up being converted into a link,
but that would be a rare occurrence. In other words, without additional
validity checking, the regex would be a best-guess procedure. For a more
strict procedure, each match could perhaps be checked against a
is_web_uri($...) function using Data::Validate::URI that validates http or
https URIs specifically. That said, any example that illustrates a basic
search and replace concept be much appreciated, even if it's only a
best-guess URL type of procedure.

Many thanks for any bright ideas!

Tuxedo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use an iframe as a link (make the whole iframe clickable) mi HTML 4 05-21-2008 10:13 PM
Clickable link in an application/applet Aaron Fude Java 3 12-29-2007 12:58 AM
HELP! When I Send Email, How Can I Make a Link/URL "Clickable" Me Computer Support 4 08-29-2006 03:01 PM
window.createPopup has a link in the popup, but this link is not clickable Jiong Feng Javascript 1 05-15-2004 09:42 AM



Advertisments