CFLib.org – Common Function Library Project

activateLinks(string)

Last updated February 22, 2011

Version: 1 | Requires: ColdFusion MX | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

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

Return Values:
Returns a string.

Example:

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

Parameters:

Name Description Required
string Input string. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson