Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hyperlinks and e-mail in string 1

Status
Not open for further replies.

LotusE

Programmer
Nov 22, 2002
25
BE
Hi,

Probably a stupid question: I have a variable in my perl script which is filled with some text (a description of a show for example). In this text there are hyperlinks, but they are displayed as common text. Is there a way to display them as a link, so that the user can click on it and go directly to the web page? I've got the same problem with e-mail adressess. The user should be able to click on them.

Thanks in advance for your answer!

Cheers

LotusE
 
Firstly, if it's a text file, then you will need to convert it to HTML before the concept of hyperlinks makes much sense. You'll also need to be viewing it in something that understands hyperlinks, such as a browser. The crudest way to convert to html would just be
Code:
print '<html><head/><body><pre>', $text, '</pre></body></html>';

To add your hyperlinks properly is a nightmare because the regex for a valid URL is pages long. You can get 95% of the way there with a set of fairly simple regexes. I've used
Code:
sub autolink {
    foreach (@_) {
        s/([\w\.]+@[\w\.]+)/mailto:$1/g; # if it looks like an email address
        s~ (mailto:)(\S+) ~ a( {-href=>"$1$2"}, $2 ) ~egx;
        s~
                ([URL unfurl="true"]http://|https://|ftp://)(\S+[/URL][^"\.\?])
            ~
                a( {-href=>"$1$2",-target=>"_new"}, "$1$2" )
            ~egx;
    }
}
in a helpdesk application. This relies on the CGI module for the a() method. If you're using that, the first code snippet becomes
Code:
autolink($text);
print start_html, pre( $text ), end_html;

Please note that this code is pragmatic rather than strictly correct.

Yours,

fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
The HTML::FromText module is useful for things like this. Here's a simple example:
Code:
#!/usr/bin/perl -w
use strict;
use HTML::FromText;

my $t2h = new HTML::FromText ({
   urls => 1,
   email => 1
});

my $text = 'some text with someone@example.com and [URL unfurl="true"]http://blah.com[/URL] in it';

print $t2h->parse( $text );
 
well, there is no way to display html encoded emails as html if the end user has their email program set to read as plain text.
 
Thanks, ishnid. A star for you and a new toy for me.

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Thanks. I actually only discovered this about two days ago!
 
Hey guys,

thanks for the overwhelming response!!!!!

I've tried ishnid's method, but it doesn't seem to work, the url's aren't replaced. I probably must have done something wrong. I've tried to fit the code in the existing code of my calendar script. The description variable is where the url's and e-mail adressess are situated:

#####################################################################
#
# Read in calendar data file
#
#####################################################################
sub read_cal_data {
undef %events;
my $id,$xdatestamp, $xmonth;
use HTML::FromText ();
use strict;
my $t2h = HTML::FromText->new ({urls => 1, email => 1});
$vars{new_id}=0;
open(IN,"$vars{calendar_file}") || &Error("Can't open $vars{calendar_file}");
my $header = <IN>;
# If file isnt in right format, give error
unless ($header =~ m/^#id\|datestamp\|label\|description/) {
close(IN);
print "The events file is not in the correct format for this version. Run

the calendar_admin script to automatically update the format.";
exit(0);
}

while (<IN>) {

chomp;
next unless /^\d/;
($id,$xdatestamp,$label,$desc) = split(/\|/,$_,4);
$xdatestamp =~ s|^0000|$vars{year}|;

if ($id > $vars{new_id}) { $vars{new_id} = $id; }

# Skip it unless it's from the current month
($xmonth = $vars{month}) =~ s|^(\d)$|0$1|;
next unless ($xdatestamp =~ m|^$vars{year}$xmonth|);

if ($vars{html_heading} ne "yes") {
$label =~ s|[<>]||g;
}
if ($vars{html_description} ne "yes") {
$desc =~ s|[<>]||g;
}

$total_events{$xdatestamp}++;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{description} =

$t2h->parse($desc);
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{label} = $label;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{id} = $id;

}
close(IN);
$vars{new_id}++;
}



&GetCwd;
&ReadParse;
 
nevermind guys, apparently, the module wasn't installed on the system, that's why nothing happened.

Thanks very much for all your help!!

Cheers

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top