?
Solved

URLs inside the output of one database field

Posted on 2003-03-12
20
Medium Priority
?
186 Views
Last Modified: 2013-12-24
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.
0
Comment
Question by:edsanz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 3
  • +3
20 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8122819
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
 

Author Comment

by:edsanz
ID: 8122897
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8123023
<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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8123029
I means </a>

<a href="www.cnn.com ">www.cnn.com </a>
0
 
LVL 8

Expert Comment

by:TallerMike
ID: 8123041
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
 
LVL 8

Expert Comment

by:TallerMike
ID: 8123211
Here, this is a start:

#ReReplaceNoCase(comment,"((http|www)[[:alpha:][:digit:].\/:]*)","<a href='\1'>\1</a>","ALL")#
0
 
LVL 8

Expert Comment

by:TallerMike
ID: 8123267
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
 
LVL 8

Accepted Solution

by:
TallerMike earned 500 total points
ID: 8123274
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
 
LVL 17

Expert Comment

by:anandkp
ID: 8125409
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
 
LVL 17

Expert Comment

by:anandkp
ID: 8125443
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
 

Author Comment

by:edsanz
ID: 8127840
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
 
LVL 8

Expert Comment

by:TallerMike
ID: 8128094
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
 
LVL 8

Expert Comment

by:TallerMike
ID: 8128264
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
 
LVL 2

Expert Comment

by:weeezl
ID: 8130818
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
 
LVL 8

Expert Comment

by:TallerMike
ID: 8130940
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
 
LVL 2

Expert Comment

by:fmedia
ID: 8146081
Some times there are very good solutions already available, like this one :

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

regrds,
idss
0
 

Author Comment

by:edsanz
ID: 8161574
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
 
LVL 8

Expert Comment

by:TallerMike
ID: 8161631
Hopefully you learned a little bit about regular expressions too?
0
 

Author Comment

by:edsanz
ID: 8219779
Yes I did. Thanks for the links to the regular expressions tutorials.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question