CFLib.org – Common Function Library Project

activateLinks(string)

Last updated February 22, 2011

author

Robin Scherberich

Version: 1 | Requires: CF6 | Library: StrLib

Description:
Converts text links into HTML links within a string. /!\ HTML links who already exists ARE'NT reconverted

Return Values:
Returns a string.

Example:

<cfoutput>
	<p>
		#activateLinks("my string with www.text-link.fr !")#
	</p>
</cfoutput>

Parameters:

Name Description Required
string Input string. Yes

Full UDF Source:

/**
 * Converts text links into HTML links within a string
 * 
 * @param string 	 Input string. (Required)
 * @return Returns a string. 
 * @author Robin Scherberich (scherberich.r@gmail.com) 
 * @version 1, February 22, 2011 
 */
function activateLinks( string )
{
	var stringLen = len( string );
	var currentPosition = 1;
	var urlArray = [];

	while( currentPosition < stringLen )
	{
		rezArray = REFindNoCase( "(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»‘’]))", arguments.string, currentPosition, true );
		
		if( rezArray.len[1] != 0 ){
			arrayAppend( urlArray, mid( string, rezArray.pos[1]-2, rezArray.len[1]+2 ) );
			currentPosition = rezArray.pos[1] + rezArray.len[1];
		} else {
			currentPosition = stringLen;
		}
	}

	for( i = 1; i <= arrayLen( urlArray ); i++ )
	{
		if( left( urlArray[i], 2 ) != '="' )
		{
			link = right( urlArray[i], len( urlArray[i] )-2 );
			string = replace( string, link, '<a href="'& link &'">'& link &'</a>', "all" );
		} else {
			i++;
		}
	}

	return string;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson