CFLib.org – Common Function Library Project

friendlyURL(title)

Last updated July 9, 2008
Download UDF

author

Nick Maloney Nick Maloney

Version: 1 | Requires: ColdFusion MX | 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:

<cfscript>
/**
* UDF that returns an SEO friendly string.
*
* @param title      String to modify. (Required)
* @return Returns a string.
* @author Nick Maloney (nmaloney@prolucid.com)
* @version 1, July 9, 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);
    }
    return lcase(title);
}
</cfscript>

Search CFLib.org


Latest Additions

Topper Topper added
getCurrentURL
8 hour(s) ago

Jeff Howden Jeff Howden added
CSVFormat
1 day(s) ago

Tom de Manincor Tom de Manincor added
flattenStruct
2 day(s) ago

Troy Pullis Troy Pullis added
iCalUS
2 day(s) ago

Christopher Jordan Christopher Jordan added
isVIN
12 day(s) ago

Created by Raymond Camden / Design by Justin Johnson