CFLib.org – Common Function Library Project

friendlyURL(title)

Last updated November 30, 2008

author

Nick Maloney

Version: 2 | Requires: CF6 | Library: UtilityLib

Description:
This function was developed to create SEO friendly urls from article titles (similar to digg and cnn). It removes all non alpha characters and replaces them with a hyphen. It also removes trailing hyphens, apostrophes, etc.

Return Values:
Returns a string.

Example:

<cfsavecontent variable="myTitle">
This wouldn't be a very SEO friendly URL, would it?
</cfsavecontent>
<cfoutput>
#friendlyUrl(myTitle)#
</cfoutput>

Parameters:

Name Description Required
title String to modify. Yes

Full UDF Source:

/**
 * UDF that returns an SEO friendly string.
 * Fix for - in front by B
 * 
 * @param title      String to modify. (Required)
 * @return Returns a string. 
 * @author Nick Maloney (nmaloney@prolucid.com) 
 * @version 2, November 29, 2008 
 */
function friendlyUrl(title) {
    title = replaceNoCase(title,"&amp;","and","all"); //replace &amp;
    title = replaceNoCase(title,"&","and","all"); //replace &
    title = replaceNoCase(title,"'","","all"); //remove apostrophe
    title = reReplaceNoCase(trim(title),"[^a-zA-Z]","-","ALL");
    title = reReplaceNoCase(title,"[\-\-]+","-","all");
    //Remove trailing dashes
    if(right(title,1) eq "-") {
        title = left(title,len(title) - 1);
    }
    if(left(title,1) eq "-") {
        title = right(title,len(title) - 1);
    }    
    return lcase(title);
}

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