URLs inside the output of one database field

I have a comment field that has paragraphs and paragraphs of information.
There may be URLs inside this text.

My question is: How can I make those URLs clickable?

This is an output example from the field [comment]:
Here is a long long paragraph with a lot of boring text and you can read more at http://www.superboringstuff.com/ and some more at www.blahblahblah.com

All of that text is in a field called [comment]. When I output this text it comes out as plain text. How can I make those two URLs clickable links? Note, one has http:// and the other one doesn't.
edsanzAsked:
Who is Participating?
 
TallerMikeConnect With a Mentor Commented:
This might work, but it's an ugly long line!

comment = ReReplaceNoCase(ReReplaceNoCase(comment,"(http[[:alpha:][:digit:].\/:]*)","<a href='\1'>\1</a>","ALL"),"(([^http:|/|/])www[[:alpha:][:digit:].]*)"," <a href='http://\1'>\1</a>","ALL");
0
 
HamdyHassanCommented:
you need to parse this comment field to looks like
<a href="URL">URL</s>

Now the question is, How to do that?

Question for you?
Could you change the program which create this comment, so instead of
URL
Ir will write
<a href="URL">URL</s>




0
 
edsanzAuthor Commented:
Your right I need to some how parse the paragraph.

I tried : <a href="#comment#">#comment#</a> and the entire paragraph becomes a link with an invalid url.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
HamdyHassanCommented:
<a> needs to be added inside the comment, not around the comment.

if commant contains
bbbbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaaaa
ccccccccccc www.cnn.com cccccccccc
dddddddddddddddddddddddddddddddddd

then you need to change the program, so it will create
bbbbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaaaa
ccccccccccc <a href="www.cnn.com ">www.cnn.com </s>cccccccccc
dddddddddddddddddddddddddddddddddd


Who generate this comment? user entry? SQL? ...
0
 
HamdyHassanCommented:
I means </a>

<a href="www.cnn.com ">www.cnn.com </a>
0
 
TallerMikeCommented:
You'll need to use a regular expression to do a replace on the comment. Let me see if I can work something up...

0
 
TallerMikeCommented:
Here, this is a start:

#ReReplaceNoCase(comment,"((http|www)[[:alpha:][:digit:].\/:]*)","<a href='\1'>\1</a>","ALL")#
0
 
TallerMikeCommented:
OK, this will do a little better, but I don't think I know how to get it into one statement...

<cfscript>
     comment="Here is a long long paragraph with a lot of boring text and you can read more at http://www.superboringstuff.com/ and some more at www.blahblahblah.com";
     comment = ReReplaceNoCase(comment,"(http[[:alpha:][:digit:].\/:]*)","<a href='\1'>\1</a>","ALL");
     comment = ReReplaceNoCase(comment,"(([^http:|/|/])www[[:alpha:][:digit:].]*)"," <a href='http://\1'>\1</a>","ALL");
</cfscript>

<cfoutput>#comment#</cfoutput>
0
 
anandkpCommented:
mike - awesome !!!

it worked perfect for me [except for one palce !]

<CFSET comment = "some text http://www.andy-akp.net & more text http://www9.akp.co.in">

the hyphen above & also underscore ... creates a small bit of error ...

this is just for ur info ... may be u cld modify it a bit & set it up to cover all possibilities of domain names ...

gr8 going :)

Cheers
Anand
0
 
anandkpCommented:
mike - awesome !!!

it worked perfect for me [except for one palce !]

<CFSET comment = "some text http://www.andy-akp.net & more text http://www9.akp.co.in">

the hyphen above & also underscore ... creates a small bit of error ...

this is just for ur info ... may be u cld modify it a bit & set it up to cover all possibilities of domain names ...

gr8 going :)

Cheers
Anand
0
 
edsanzAuthor Commented:
TallerMike, great code!
It worked like a charm.

I'm going to leave the thread unlocked for a couple of days so we can discuss further.  I think anandkp has a point there.

I'm also going to work on changes to make this work with email addresses links.

Where can I get more information on the meaning of [:alpha:][:digit:] and \1 ? My reference manuals don't cover that.

Thank you.
0
 
TallerMikeCommented:
Whose question is this anyhow?  =)

It all depends on how you want to set it up. I have it setup to match the following (in the first replace), which basically says "Match all 'http' followed by any number of numbers, digits, periods, slashes, or colons"

(http[[:alpha:][:digit:].\/:]*)

The second replace matches the following: "Match all non 'http' followed by 'www' and then any number of numbers, digits, or periods"

([^http:|/|/])www[[:alpha:][:digit:].]*)

So basically your options are to add each of the specific characters that you want to allow to the end of the list (basically before the ]*) in each statement) while being careful to escape the neccesary characters... you could continue on this inclusive model.

Alternatively you could attempt an exclusive model, something like this (this is NOT tested by the way) that would say "Match all 'http' followed by any number of characters that are not spaces or commas"

(http://[^ ,]*)

And for the www end, something like this:

(([^http:|/|/])www[^ ,]*)


Anyhow, there's still other situations to consider. What if they put the link at the end of a sentence like so:

I like  http://www.yahoo.com.
0
 
TallerMikeCommented:
Try these on for size:

comment = ReReplaceNoCase(comment,"(http://[^ ]*[^\. ])","<a href='\1'>\1</a>","ALL");
     comment = ReReplaceNoCase(comment,"(([^http:|/|/])www[^ ,]*[^\. ])"," <a href='http://\1'>\1</a>","ALL");

They shuld account for the period at the end of a sentence, unlike the regular expression on EE which does not, notice the following link:

I like http://www.yahoo.com.
0
 
weeezlCommented:
A simple alternative:

http://www.cfcomet.com/utilities/CF_LinkFinder_v1-1.zip

This utility will turn URLs within messages into active URLs and will make email addresses active mailto links as well.

Have fun!
0
 
TallerMikeCommented:
This script uses the same method of regular expression to replace the links. They may have some more selective expressions than the ones I have above.

These are the scripts they use:

<CFSCRIPT>
    this = Attributes.Data;
     this = REReplaceNoCase(this, "(((file:///)|([a-z]:\\)|(\\\\[[:alpha:]]))+(\.?[[:alnum:]\/=^@*|:~`+$%?_##& -])+)", "<A TARGET=""_blank"" HREF=""\1"">\1</A>", "ALL");
     this = REReplaceNoCase(this, "([[:alnum:]]*://[[:alnum:]\@-]*(\.[[:alnum:]][[:alnum:]-]*[[:alnum:]]\.)?[[:alnum:]]{2,}(\.?[[:alnum:]\/=^@*|:~`+$%?_##&-])+)", "<A TARGET=""_blank"" HREF=""\1"">\1</A>", "ALL");
     this = REReplaceNoCase(this, "(([[:alnum:]][[:alnum:]_.-]*)?[[:alnum:]]@[[:alnum:]][[:alnum:].-]*\.[[:alpha:]]{2,})", "<A HREF=""mailto:\1"">\1</A>", "ALL");
</CFSCRIPT>

The first being for file links (not something you need), the second for web links (not sure it will handle www, it may need the http part as well), and the third for email address links.
0
 
fmediaCommented:
Some times there are very good solutions already available, like this one :

http://www.cflib.org/udf.cfm?ID=691

regrds,
idss
0
 
edsanzAuthor Commented:
Thanks to everyone who answered my question.
I'm giving tallermike the points because he was the first to answer my original question.

Thanks to all those who gave me links to other valid solutions!
0
 
TallerMikeCommented:
Hopefully you learned a little bit about regular expressions too?
0
 
edsanzAuthor Commented:
Yes I did. Thanks for the links to the regular expressions tutorials.
0
All Courses

From novice to tech pro — start learning today.