Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Creating links in the URLs in a text paragraph

by Anonymous Monk
on Jun 25, 2007 at 03:16 UTC ( [id://623099]=perlquestion: print w/replies, xml ) Need Help??

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

how can I take a paragraph in text format, and make any http:// url's clickable in HTML?

I have searched for Links and stuff, but cannot find anything on this particular search. I know how to tell if http:// is present in the text, but now how to make it a clickable link.

Thank you.

  • Comment on Creating links in the URLs in a text paragraph

Replies are listed 'Best First'.
Re: Creating links in the URLs in a text paragraph
by Zaxo (Archbishop) on Jun 25, 2007 at 04:19 UTC

    You can use Regexp::Common::URI to detect the the urls in plain text and perform a substitution. Assuming the text is in $_, the following will wrap with anchor tags, with the host as text.

    use CGI qw/:html/; use Regexp::Common qw/URI/; s/$RE{URI}{HTTP}/a( {href => $1}, $3)/eg;

    Untested. The /e flag on the substitution executes the CGI::a() function to obtain the substitute. The /g flag makes the substitution for all recognised urls in the text.

    After Compline,
    Zaxo

Re: Creating links in the URLs in a text paragraph
by naikonta (Curate) on Jun 25, 2007 at 03:33 UTC
    Something like,
    if ($this_portion_of_string_is_a_url) { $this_portion_of_string_is_a_url = qq(<a href="$this_portion_of_string_is_a_url">$this_portion_of_str +ing_is_a_url</a>); }
    ?

    But, you may want to borrow from Pod::Html a subroutine called make_URL_href:

    sub make_URL_href($){ my( $url ) = @_; if( $url !~ s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){ $url = "<a href=\"$url\">$url</a>"; } return $url; }

    Update: fixed paragraph


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      So, what you are saying is that I can take something like this:

      $_pageOfContent = qq~ Hello my name is so and so, and I really recommend this website becaus +e I think it has some really cool ideas, you can find it at the websi +te located on http://www.mydom.com and also on this other one here ht +tp://www.yourdom.com. Thank you much, Me~; $_pageOfContent = make_URL_href($_pageOfContent); # Now $_pageOfContent will have this content: #Hello my name is so and so, and I really recommend this #website beca +use I think it has some really cool ideas, #you can find it at the we +bsite located on #<a href="http://www.mydom.com">http://www.mydom.com</a> #and also on +this other one here #<a href="http://www.yourdom.com">http://www.yourdom.com</a>. # #Thank you much, #Me #? sub make_URL_href($){ my( $url ) = @_; if( $url !~ s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){ $url = "<a href=\"$url\">$url</a>"; } return $url; }
      Is that right?
      thank you.

        Why don't you just try it and see if it works? At last count very few computers went supercritical and blew up from running a piece of Perl-script. It is quite safe to do, you know.

        CountZero

        "It can't get any worse
        ****BOOOM****
        It got worse."

Re: Creating links in the URLs in a text paragraph
by GrandFather (Saint) on Jun 25, 2007 at 03:31 UTC

    See http://www.w3.org/TR/html401/struct/links.html for a description of links in HTML 4.01 documents.

    Update:

    If you are using CGI then you can:

    print $q->p ( "This is a paragraph including a link: ", $q->a({href=>"http://perlmonks.org/index.pl?node_id=623101"},"Samp +le link"), " to the node containing this sample." );

    DWIM is Perl's answer to Gödel
Re: Creating links in the URLs in a text paragraph
by merlyn (Sage) on Jun 25, 2007 at 04:50 UTC
Re: Creating links in the URLs in a text paragraph
by Anonymous Monk on Jun 25, 2007 at 05:02 UTC
      Thank you Zaxo and Anon.
      sub GetLinks { $text = shift; use Regexp::Common; $text =~ s[($RE{URI}{HTTP})] [<a href = "$1" target="_blank">$1</a>]g; return($text); }
      that subroutine works...

      Many a thanks!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://623099]
Approved by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-12-09 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (53 votes). Check out past polls.