CFLib.org – Common Function Library Project

friendlyURL(title)

Last updated November 29, 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);
}
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