My search on CFLIB didn't produce a match for a UDF to replace URLs with an HTML link for that URL, so I'm going to submit one as soon as I convert my CFSCRIPT version to standard ColdFusion tag equivalent.

In the mean time, below is the custom function that I use to automatically convert URLs in my blog comments to a link so readers can just click through. I strip HTML and CFML tags from user comments rather than convert the tags to text with htmleditformat(), so a user cannot insert anchor tags for a link with their comment.

ReplaceLinks(String)

view plain print about
1<br/><br/><br/><br/>[cffunction name="replaceLinks" access="public" output="yes" returntype="string">
2 [cfargument name="input" required="Yes" type="string">
3 [cfscript>
4 var inputReturn = arguments.input;
5 var pattern = "";
6 var urlMatches = structNew();
7 var inputCopy = arguments.input;
8 var result = "";
9 var rightStart = "";
10 var rightInputCopyLen = "";
11 var targetNameMax = "";
12 var targetLinkName = "";
13 var i = "";
14
15 pattern = getURLRegExPattern();
16 while (len(inputCopy)) {
17 result = refind(pattern,inputCopy,1,'true');
18 if (result.pos[1]){
19 match = mid(inputCopy,result.pos[1],result.len[1]);
20 urlMatches[match] = "";
21 rightStart = result.len[1] + result.pos[1];
22 rightInputCopyLen = len(inputCopy)-rightStart;
23 if (rightInputCopyLen GT 0){
24 inputCopy = right(inputCopy,rightInputCopyLen);
25 } else break;
26 } else break;
27 }
28
29 urlMatches = structKeyArray(urlMatches);<br/><br/> targetNameMax = 50;
30 for (i=1; i LTE arraylen(urlMatches);i=i+1) {
31 targetLinkName = urlMatches[i];
32 if (len(targetLinkName) GTE targetNameMax) {
33 targetLinkName = left(targetLinkName,targetNameMax) & "...";
34 }
35 inputReturn = replace(inputReturn,urlMatches[i],'<a href="#urlMatches[i]#" target="_blank">#targetLinkName#</a>',"all");
36 }
37 [/cfscript>
38 [cfreturn inputReturn>
39[/cffunction><br/><br/>[cffunction name="getURLRegExPattern" access="public" output="No" returntype="string">
40 [cfreturn "(http|https|ftp)://[-w.]+(:d+)?(/([w/_.-##+@]*(?S+)?)?)?">
41[/cffunction><br/><br/>


Sample Usage
[cfsavecontent variable="sample">
Enim nulla nostrud nisl in illum in duis ea (http://www.talkingtree.com/blog/index.cfm/2006/1/20/UDF-to-replace-URLs-with-a-hyperlinked-equivalent). Facilisi in minim iusto. Consequat dignissim, diam feugiat facilisis suscipit ex vulputate.
[/cfsavecontent>
[cfoutput>#replaceLinks(sample)#[/cfoutput>


Result
Enim nulla nostrud nisl in illum in duis ea (http://www.talkingtree.com/blog/index.cfm/2006/1/2...). Facilisi in minim iusto. Consequat dignissim, diam feugiat facilisis suscipit ex vulputate


Although it just occurred to me that the code used to replace the above code blocks to nicely formatted form fields must use a similar function because it kept making a link of my sample usage text which contained a URL ... Doh! At least this was a good exercise for me.