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

Hello,

I have this text field thats 255 characters max. Now the fields may have URL's and I was wondering if anyone can give me some sort of link or guidence on where I can learn how I can automatically convert those URL's into links printed in HTML.

The text field will be stored inside a database and when I print the comments onto the browser, I would automatically want the URL's to be a link. I'm figuring some sort of regex may do the trick? Finding "http://" to the next space and then taking that value and printing it in "a" tags.

thanks for taking your time to read this.

Considered: ww: retitle: hyperlinks in text fields (not comments as in # nor as in <!-- nor /*..... etc)
Unconsidered: ysth - Keep/Edit/Delete: 7/22/0

Replies are listed 'Best First'.
Re: Having hyperlinks in comments
by BUU (Prior) on Jun 25, 2005 at 07:40 UTC

      I was going to propose, as one more alternative, Regexp::Common, but when I did quick test of it, I discovered that it gives somewhat wrong undesired results with some URLs (note the trailing non-URL characters parentheses, commas, semicolons, etc. in some of returned URLs):

      % wget -qO - http://www.ebay.com | perl -MRegexp::Common=URI -wnle 'print $1 while /($RE{URI}{HTTP})/g'|h +ead http://include.ebaystatic.com/js/v/us/homepage.js http://include.ebaystatic.com/aw/pics/us/css/homepage.css http://pics.ebaystatic.com/aw/pics/userSitePrefs/bottomDropShadow_20x2 +0.gif) http://pics.ebaystatic.com/aw/pics/userSitePrefs/sideDropShadow_20x20. +gif) http://pics.ebaystatic.com/aw/pics/userSitePrefs/dropshadow2_20x10.gif +) http://include.ebaystatic.com/aw/pics/css/ebay.css http://include.ebaystatic.com/'; http://include.ebaystatic.com/js/v/us/ebaybase.js http://include.ebaystatic.com/js/v/us/ebaysup.js http://search.ebay.com/',
      ...while URI::Find::Rule does a better job DWIM:
      % wget -qO - http://www.ebay.com | perl -MURI::Find::Rule -wlne ' print $_->[1] for URI::Find::Rule->scheme("http")->in($_)'|head http://include.ebaystatic.com/js/v/us/homepage.js http://include.ebaystatic.com/aw/pics/us/css/homepage.css http://pics.ebaystatic.com/aw/pics/userSitePrefs/bottomDropShadow_20x2 +0.gif http://pics.ebaystatic.com/aw/pics/userSitePrefs/sideDropShadow_20x20. +gif http://pics.ebaystatic.com/aw/pics/userSitePrefs/dropshadow2_20x10.gif http://include.ebaystatic.com/aw/pics/css/ebay.css http://include.ebaystatic.com/ http://include.ebaystatic.com/js/v/us/ebaybase.js http://include.ebaystatic.com/js/v/us/ebaysup.js http://search.ebay.com/

      Update: Fixed the incorrect wording. As merlyn pointed out, the unwanted trailing characters are valid URL characters. Still I think they could be a problem in the case of the application the OP described. Therefore, in this case, R::C is not the most straighforward solution.

      the lowliest monk

Re: Having hyperlinks in comments
by ikegami (Patriarch) on Jun 25, 2005 at 05:16 UTC

    When people type URLs into messages, they frequently follow it with punctuation characters, so s{(http://\S+)}{...}g will often grab too much. I'd start with:

    s{( http:// [^\]\})>\s]+ # Won't allow any of these. (?<=[a-zA-Z0-9/&=]) # Must end with one of these. )}{ '<a href="' . escape_html_value($1) . '">' . escape_html_text($1) . '</a>' }xeg; sub escape_html_value # Escapes at least: & => &amp; " => &quot; sub escape_html_text # Escapes at least: & => &amp; < => &lt;

    Adjust at will.

Re: Having hyperlinks in comments
by TedPride (Priest) on Jun 25, 2005 at 05:52 UTC
    Problem is, not all links start with http://. There are other protocols as well, and someone could forget the http://. Also, what if it's a link with arguments attached? That won't necessarily end with an alphanumeric character. URLs come in so many formats that it's really much better to just do like Perlmonks does and have links be marked by enclosing them in brackets. Either that or use a module specifically designed for finding URLs inside a block of text, but I wouldn't know where to look for one of those.
Re: Having hyperlinks in comments
by Anonymous Monk on Jun 25, 2005 at 05:08 UTC
    sounds like a good idea to me. I'd go with that regex.