Coldfusion 9 find web address in string  and change it to html hyperlink

smares323
smares323 used Ask the Experts™
on
Coldfusion 9 find web address in text string and change it to html hyperlink.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015

Commented:
Can you give an example of the actual "string" and the expected output?

Author

Commented:
coldfusion findnocase web address in text string and replace with <a href="web address">web address</a>
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015

Commented:
Right, but are you searching for only "http://www.google.com" - or do you want to find any URL, like find "http://http://www.experts-exchange.com/", http://bing.com, etc... ? If you're trying to find any URL, you'll need to use a Regex. That's which not my area of expertise.  

Not sure if you can modify the question to add topics. If not, submit a "Request for Attention" and ask a moderator to add the regex topic to the question.

Author

Commented:
Find any url in the text string,
Most Valuable Expert 2015

Commented:
Ok.  Unfortunately, regex's aren't my strong suit.  I submitted a "Request for a Attention" and asked the mods to add that zone to your question to attract answers from a regex expert.
you can try this:

http://www.cflib.org/udf/activateurl

this is what exactly you want
do read the comments also, regex option is also provided

Author

Commented:
None of the examples are working.
here is an example how to do it using the library

<cfscript>
function ActivateURL(string) {
	var nextMatch = 1;
	var objMatch = "";
	var outstring = "";
	var thisURL = "";
	var thisLink = "";
	var	target = IIf(arrayLen(arguments) gte 2, "arguments[2]", DE(""));
	var paragraph = IIf(arrayLen(arguments) gte 3, "arguments[3]", DE("false"));
	
	do {
		objMatch = REFindNoCase("(((https?:|ftp:|gopher:)\/\/)|(www\.|ftp\.))[-[:alnum:]\?%,\.\/&##!;@:=\+~_]+[A-Za-z0-9\/]", string, nextMatch, true);
		if (objMatch.pos[1] GT nextMatch OR objMatch.pos[1] EQ nextMatch) {
			outString = outString & Mid(String, nextMatch, objMatch.pos[1] - nextMatch);
		} else {
			outString = outString & Mid(String, nextMatch, Len(string));
		}
		nextMatch = objMatch.pos[1] + objMatch.len[1];
		if (ArrayLen(objMatch.pos) GT 1) {
			// If the preceding character is an @, assume this is an e-mail address
			// (for addresses like admin@ftp.cdrom.com)
			if (Compare(Mid(String, Max(objMatch.pos[1] - 1, 1), 1), "@") NEQ 0) {
				thisURL = Mid(String, objMatch.pos[1], objMatch.len[1]);
				thisLink = "<A HREF=""";
				switch (LCase(Mid(String, objMatch.pos[2], objMatch.len[2]))) {
					case "www.": {
						thisLink = thisLink & "http://";
						break;
					}
					case "ftp.": {
						thisLink = thisLink & "ftp://";
						break;
					}
				}
				thisLink = thisLink & thisURL & """";
				if (Len(Target) GT 0) {
					thisLink = thisLink & " TARGET=""" & Target & """";
				}
				thisLink = thisLink & ">" & thisURL & "</A>";
				outString = outString & thisLink;
				// String = Replace(String, thisURL, thisLink);
				// nextMatch = nextMatch + Len(thisURL);
			} else {
				outString = outString & Mid(String, objMatch.pos[1], objMatch.len[1]);
			}
		}
	} while (nextMatch GT 0);
		
	// Now turn e-mail addresses into mailto: links.
	outString = REReplace(outString, "([[:alnum:]_\.\-]+@([[:alnum:]_\.\-]+\.)+[[:alpha:]]{2,4})", "<A HREF=""mailto:\1"">\1</A>", "ALL");
		
	if (paragraph) {
		outString = ParagraphFormat(outString);
	}
	return outString;
}

</cfscript>

<cfset str = "Hello friends, here i am doing a work where i am trying to www.google.com and checking and if i can open www.yahoo.com where www.ca.yahoo.com is opening or not. i found one website which is www.retailmenot.com for coupons when you shop online so was trying if i can use that www.website.com to use it here">

<cfoutput>#activateurl(str)#</cfoutput>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial