sluggify(str[, spacer])
Last updated June 11, 2009
Version: 1 | Requires: CF6 | Library: StrLib
Description:
This function will take any string and replace all of the non-alphanumeric characters with the spacer of your choice (default is '-').
Return Values:
Returns a string.
Example:
<cfset toBeSlugged = "This is a (very) nonalphanumeric, string" />
<cfset slug = sluggify(toBeSlugged) />
<cfoutput>#slug#</cfoutput>
Parameters:
Name | Description | Required |
---|---|---|
str | String to modify. | Yes |
spacer | Character used for spaces. Defaults to -. | No |
Full UDF Source:
<!---
Converts a string into a pretty URL safe slug
@param str String to modify. (Required)
@param spacer Character used for spaces. Defaults to -. (Optional)
@return Returns a string.
@author Michael Haggerty (mike@mikehaggerty.net)
@version 1, June 11, 2009
--->
<cffunction name="sluggify" output="false" returnType="string">
<cfargument name="str">
<cfargument name="spacer" default="-">
<cfset var ret = "" />
<cfset str = lCase(trim(str)) />
<cfset str = reReplace(str, "[^a-z0-9-]", "#spacer#", "all") />
<cfset ret = reReplace(str, "#spacer#+", "#spacer#", "all") />
<cfif left(ret, 1) eq "#spacer#">
<cfset ret = right(ret, len(ret)-1) />
</cfif>
<cfif right(ret, 1) eq "#spacer#">
<cfset ret = left(ret, len(ret)-1) />
</cfif>
<cfreturn ret />
</cffunction>
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