Gossamer Forum
Home : General : Perl Programming :

String of links to HTML - Subsitution?

Quote Reply
String of links to HTML - Subsitution?
I have a string which contains anywhere from 0 to multiple webpage addresses, which I want to convert to HTML clickable links when I print the string.

Example: $before = 'http://www.mysite.com http://theotherone.co.uk http://www.maybeantother';

$after = '<a href = "http://www.mysite.com">http://www.mysite.com </a> etc';

Regardless of spaces between URL, or the number of URLS (inlcuding 0).

I guess this is just a "convert anything that looks like a URL to a URL'.

I have looked in the GT Docs and found things like deparse_url, which does not seem to be what I want.

I have also found various functions by searching the net, but none seem to exactly match this.

Example junk:

Code:
# hyperlink: Generate HTML link
sub hyperlink
{
$_ = shift;
s/&/&amp;/g;
s/</&lt;/g;
my $new;
while (m/=\?iso-8859-1\?Q\?([^\?]*)\?=/)
{
$new = $1;
$new =~ s/=(\w\w)/"&#".hex($1).";"/gie; # Convert quoted-printable
s/=\?iso-8859-1\?Q\?[^\?]*\?=/$new/;
}
s&(ftp|gopher|http)(://[^<>),"\&\s]*[^<>),."\&\s])&<A HREF="$1$2">$1$2</A>&g;
s&(mailto|news|telnet)(:[^<>)\],"\s]+)&<A HREF="$1$2">$1$2</A>&g;
s&(^|\s)(ftp|gopher)(\.[\w./-]+)(\s|$)&$1<A HREF="$2://$2$3/">$2$3</A>$4&g;
s&(^|\s)(www\.[\w./-]+)(\s|$)&$1<A HREF="http://$2/">$2</A>$3&g;
s&(article|In-Reply-To:|Message-ID:|References:)(\s+\&lt;)([^@>"\s]+@[^>"\s]*)&$1$2<A HREF="$3">$3</A>&gi;
s/(^|^"|[^=]"|SMTP:|[()\[\]\s]|(&lt;)+)([^&()>":\[\]\s]+@)([\w\-.]+\.[\w\-]+|\[\d+\.\d+\.\d+\.\d+\])/$1<A HREF="mailto:$3$4">$3$4<\/A>/g;
return $_;
}


Is there a perl module that this is already built into? or better still a simple regex or function I could use?


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 29, 2002, 2:21 PM
Quote Reply
Re: [Ian] String of links to HTML - Subsitution? In reply to
Heh try that long regex with https://www.foo.com and it won't work Wink

Here is a simple one...do you want it to match http:// or things like ftp:// too?....this will do URL's...

(Btw that code you pasted is for hyperlinking URL's in emails I think).

Code:

my $string = 'http://www.wiredon.net http://www.iuni.com ftp://cpan.org http://www.aol.com/foo/bar.html';

$string =~ s{((?:https?|ftp|telnet)://(?:[\w-]+\.)?[\w-]+\.\S+)}{<A HREF="$1">$1</A>}g;

Not perfect I expect but it works :)

Last edited by:

Paul: Jun 30, 2002, 2:32 AM
Quote Reply
Re: [Paul] String of links to HTML - Subsitution? In reply to
Paul

You should also allow for ~ in URLs.

- wil
Quote Reply
Re: [Wil] String of links to HTML - Subsitution? In reply to
>>You should also allow for ~ in URLs.<<

Not in the domain name I shouldn't. After the domain, yes, but the code already allows them.
Quote Reply
Re: [Paul] String of links to HTML - Subsitution? In reply to
Does it?

- wil
Quote Reply
Re: [Wil] String of links to HTML - Subsitution? In reply to
Yeah, try it Tongue
Quote Reply
Re: [Paul] String of links to HTML - Subsitution? In reply to
Hi Paul,

Thanks! I'll give that a goSmile

Quote:


or things like ftp:// too?....


Actually this was not necessary, but since its there, thank you!

Quote:


(Btw that code you pasted is for hyperlinking URL's in emails I think).


Yeah I had a feeling it was something to do with that! I was out of the guts of a fairly lengthy freeware code somewhere. (from memory - last night seems so far away!)


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 30, 2002, 7:57 AM
Quote Reply
Re: [Ian] String of links to HTML - Subsitution? In reply to
You are up early...must be 6am?

I've played a round of golf already Cool

Edit: Its 8am I think not 6?

Last edited by:

Paul: Jun 30, 2002, 7:59 AM
Quote Reply
Re: [Paul] String of links to HTML - Subsitution? In reply to
Yeah, you would think I pooped the bed! Actually my g/f had to get up for work (she works shifts). So you know how addicting this forum can be - I was all keen to see if there was a reply to my regex questionTongue.

No golf here today... its pouring rain.... but we don't seem to get much in the way of lightening, so I guess you could still safely swing the iron.


Edit: yep 8am!


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 30, 2002, 8:02 AM
Quote Reply
Re: [Ian] String of links to HTML - Subsitution? In reply to
>>
Yeah, you would think I pooped the bed!
<<

What a pretty picture.

>>
Actually my g/f had to get up for work (she works shifts).
<<

Ahh you were all asleep whilst Brazil kicked ass :)

>>
So you know how addicting this forum can be
<<

No?...the forum sucks...I rarely visit it. Laugh

>>
I was all keen to see if there was a reply to my regex question.
<<

Yeah I like waking up to replies....so like to return the favor Wink

Last edited by:

Paul: Jun 30, 2002, 8:06 AM
Quote Reply
Re: [Paul] String of links to HTML - Subsitution? In reply to
LOL

Well, I must admit I checked the cup resuts before typing http://www.gossamer-threads.com/perl/gforum/gforum.cgi?


http://www.iuni.com/...tware/web/index.html
Links Plugins