CFLib.org – Common Function Library Project

activateLinks(string)

Last updated February 23, 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson