SysAdm has asked for the wisdom of the Perl Monks concerning the following question:

Need to read a data file and return all www. as a hyper-link...

I figured the email one would be the same code just search for different character - @

My only problem is how would you read in the www.string and display it as a hyper-link...??? Could you use the $_ to display the found string????

___________SysAdm

Replies are listed 'Best First'.
Re: Returning data as a hyper link
by lhoward (Vicar) on Mar 13, 2001 at 09:39 UTC
    The HTML::FromText module is designed to convert flat text files to HTML. Among its options are:
    • urls - Convert URLs to links
    • email - Convert email addresses to links
Re: Returning data as a hyper link
by $code or die (Deacon) on Mar 13, 2001 at 09:54 UTC
    Apart from the advice you have already had about URLs, don't write a regular expression to find email addresses - use Email::Find

    Update: lhoward's advice is good. Be careful if your input is HTML because your regular expression will display the link twice if it's html that already has links. e.g.:
    <a href="http://www.perlmonks.org">http://www.perlmonks.org</a>
    With arturo's regular expression, you will get:
    <a href="http://<a href="www.perlmonks.org"></a>">http://<a href="www. +perlmonks.org"></a></a>
    or something like that.

    $ perldoc perldoc
Re: Returning data as a hyper link
by arturo (Vicar) on Mar 13, 2001 at 04:46 UTC

    I'd use a regular expression and capture the text. Depending on how serious you are about getting actual URL-friendly characters, something as simple as (heh):

    # $text contains the text to be 'urled' $text=~ s/(www\.\S+)/<a href="http:\/\/$1">$a<\/a>/g;
    might do you just fine. (explanation: looks for "www" followed by a period and one or more non-whitespace characters. remembers all of that. $1 is where the memorized value goes. The rest is left as an exercise for the reader).

    search keywords: regular, expression, memory

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Returning data as a hyper link
by Desdinova (Friar) on Mar 13, 2001 at 09:32 UTC
    I dont what your data source is but if there is any chance it might include things like space you hvae to encode those properly as well More info can be found here - Insert URL Encoding