Aoccdrnig To Rscheearch - ColdFusion UDF To Garble Words

Posted September 24, 2007 at 4:50 PM

Tags: ColdFusion

Earlier today, I was talking about the clever SPAM mail I got; it used randomly inserted characters to prevent anti-spam engines from matching banned words while at the same time keeping the text human-readable. This made me think of that popular email that went around a while back that demonstrated that as long as the first and last letters of each word were kept in place, the rest of the letters could be shuffled and it would still be human-readable (for the most part).

Here is the example that made the email rounds:

Aoccdrnig to rscheearch at Cmabrigde uinervtisy, it deosn't mttaer waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteres are at the rghit pclae. The rset can be a tatol mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.

I had some time at lunch and thought it would be fun to turn this into a ColdFusion user defined function: GarbleText(). This takes one argument, the target text, and shuffles the letters of each word, leaving the first and last letter of each word the same. Therefore, the only words that will be shuffled will be those consisting of at least 4 letters:

(first:1 letter)(middle: 2+ letters)(last:1 letter)

I perform this using Java regular expressions and the special character set: \w (word characters). This means that in addition to first and last letters, all punctuation will be kept in the same place (as it is not considered a word character.

Here is the GarbleText() ColdFusion user defined function:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="GarbleText"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="This takes a string and garbles the text leaving the start and end characters in place.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Text"
  • type="string"
  • required="true"
  • hint="The text that we are going to garble."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!---
  • Define a pattern that we are going to use to find the
  • text that we can garble. Our patternw will require us
  • to have at least two characters between the start
  • and end characters.
  • --->
  • <cfset LOCAL.Pattern = CreateObject(
  • "java",
  • "java.util.regex.Pattern"
  • ).Compile(
  • "(?<=\b\w)(\w{2,})(?=\w\b)"
  • ) />
  •  
  • <!--- Create a pattern matcher for our regex. --->
  • <cfset LOCAL.Matcher = LOCAL.Pattern.Matcher(
  • ARGUMENTS.Text
  • ) />
  •  
  • <!---
  • Create the text buffer that we are going to use to
  • build the garbled text.
  • --->
  • <cfset LOCAL.Buffer = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ).Init()
  • />
  •  
  • <!---
  • Create a static instance of the collections utility so
  • that we can easily shuffle our substrings.
  • --->
  • <cfset LOCAL.Collections = CreateObject(
  • "java",
  • "java.util.Collections"
  • ) />
  •  
  • <!---
  • Now, use the pattern matcher to loop over the matching
  • substrings that we are going to garble.
  • --->
  • <cfloop condition="LOCAL.Matcher.Find()">
  •  
  • <!--- Get the matching substring. --->
  • <cfset LOCAL.Match = LOCAL.Matcher.Group(
  • JavaCast( "int", 0 )
  • ) />
  •  
  • <!--- Create an array for the characters. --->
  • <cfset LOCAL.Chars = ArrayNew( 1 ) />
  •  
  • <!--- Build out the characters array. --->
  • <cfloop
  • index="LOCAL.CharIndex"
  • from="1"
  • to="#Len( LOCAL.Match )#"
  • step="1">
  •  
  • <!--- Add character to array. --->
  • <cfset ArrayAppend(
  • LOCAL.Chars,
  • Mid( LOCAL.Match, LOCAL.CharIndex, 1 )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Shuffle the character array. --->
  • <cfset LOCAL.Collections.Shuffle(
  • LOCAL.Chars
  • ) />
  •  
  • <!---
  • Add the garbled substring back into the string
  • buffer in our ongoing result. As we do this, convert
  • the character array to a string (we don't need to
  • worry about special characters since they were not
  • matched in the original pattern.
  • --->
  • <cfset LOCAL.Matcher.AppendReplacement(
  • LOCAL.Buffer,
  • ArrayToList( LOCAL.Chars, "" )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • Now that we have garbled all of the matchign substrings,
  • we have to add the rest of the text that succeeded the
  • last match.
  • --->
  • <cfset LOCAL.Matcher.AppendTail(
  • LOCAL.Buffer
  • ) />
  •  
  •  
  • <!--- Return the garbled text. --->
  • <cfreturn LOCAL.Buffer.ToString() />
  • </cffunction>

Taking this ColdFusion UDF, we can then pass in some text to get a garbled response:

 Launch code in new window » Download code as text file »

  • <!--- Store some text that we want to garble. --->
  • <cfsavecontent variable="strText">
  • This is quite embarrassing to admit, but I have been
  • totally checking you out from across the room. I don't
  • think that I have ever seen anyone look so amazingly
  • sexy in a dress like that. It's not like me to just
  • come out and say something to that effect, but your
  • legs, your curves, your beauty - I seem to be unable
  • to control myself.
  • </cfsavecontent>
  •  
  • <!--- Output the garbled text. --->
  • #GarbleText( strText )#

Running the above code, we get this garbled output:

Tihs is quite ebnraasrsimg to adimt, but I have been tltloay ckhniceg you out form aroscs the room. I don't thnik taht I have eevr seen aynnoe look so alamgziny sxey in a dress lkie that. It's not like me to just come out and say shnoitemg to that ecfeft, but your lges, your cveurs, yuor btuaey - I seem to be unalbe to cootrnl mlesyf.

Fun times. Try out the GarbleText() UDF for yourself.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 24, 2007 at 9:49 PM // reply »
11 Comments

Tkhnas for sahring tihs ilecbirdny uslsees fcuitnon wtih the rset of the cfiolosudn wolrd. I am srue my clehdirn wlil lvoe it.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »