CFLib.org – Common Function Library Project

naughtyFilter(body[, replaceType][, repeatValue][, naughtyList][, replaceString])

Last updated August 17, 2004

author

Chris Wigginton

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Replaces dirty words in text using naughtyList (Carlin's 7 dirty words as an example) with either middle characters only or full replacement of text.

Return Values:
Returns a string.

Example:

<cfset testTxt ="George Carlin's 7 Dirty words are shit piss fuck cunt cocksucker, mother fucker and tits">

<cfoutput>#naughtyFilter(testTxt,"FL")#</cfoutput>

Parameters:

Name Description Required
body String to check. Yes
replaceType If ALL, all characters are replaced. If FL, only the middle characters are replaced. Defaults to ALL. No
repeatValue String to use for replacement when replaceType is FL. Defaults to * No
naughtyList Default list of curse words. No
replaceString Used for replacements when replaceType is ALL. No

Full UDF Source:

<!---
 Replaces dirty words with pattern.
 
 @param body 	 String to check. (Required)
 @param replaceType 	 If ALL, all characters are replaced. If FL, only the middle characters are replaced. Defaults to ALL. (Optional)
 @param repeatValue 	 String to use for replacement when replaceType is FL. Defaults to * (Optional)
 @param naughtyList 	 Default list of curse words. (Optional)
 @param replaceString 	 Used for replacements when replaceType is ALL. (Optional)
 @return Returns a string. 
 @author Chris Wigginton (c_wigginton@yahoo.com) 
 @version 1, November 30, 2004 
--->
<cffunction name="naughtyFilter"  returntype="string" hint="Replaces unmentionables with gobbledegook">
	<cfargument name="body" type="string" required="yes" hint="Contains text to filter">
	<cfargument name="replaceType" default="all" hint="ALL - all characters, FL - only the middle bits are replaced">
	<cfargument name="repeatValue" default="*" hint="Character to repeat for replaced dirty characters">
	<cfargument name="naughtyList" default="mother fucker,cocksucker,shit,piss,fuck,cunt,tits" hint="George Carlin's original 7 dirty words, not in his original order, but the longest listed first">
	<cfargument name="replaceString" default="!@##$%^&*()!@##$%^&*()!@##$%^&*" hint="A replace string for ALL method, length must be at least as long as the longest dirty word">
	<cfset var naughtyWord = "">
	<cfset var replacement = "">

	<cfloop list="#Arguments.naughtyList#" index="naughtyWord" delimiters=",">
		<cfswitch expression="#UCase(Arguments.replaceType)#">
			<cfcase value="FL">
				<cfif Len(naughtyWord) GTE 3>
					<cfset replacement = Left(naughtyWord,1) & RepeatString(Arguments.repeatValue,Len(naughtyWord) - 2) & Right(naughtyWord,1)>
				<cfelse>
					<cfset replacement = RepeatString(Arguments.repeatValue,Len(naughtyWord))>
				</cfif>	
			</cfcase>
			<cfdefaultcase>
					<cfset replacement = Left(Arguments.replaceString,Len(naughtyWord))>
			</cfdefaultcase>

		</cfswitch>
		<cfset Arguments.body = REReplaceNoCase(Arguments.body,naughtyWord,replacement, "ALL")>
	</cfloop>
	<cfreturn Arguments.body>
</cffunction>
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