Another option for trimming text in ColdFusion

This post is more than 2 years old.

Yesterday I wrote a blog entry discussing how you can trim text in ColdFusion without cutting words off in the middle. I mainly focused on the handy udf FullLeft(), which made the task easier.

Today I was in a church service (my son's school was closing today - don't worry - I haven't turned pious all of a sudden) and noticed something odd. On the bulletin for the program, they listed some text with a split like so:

Insert some bible text here. I guess I'm a sucky Catholic as I can't remember any bible verses. ... The end of the text is here. Blah blah lorem ipsum deloras.

Notice that instead of doing "Left text...", they trimmed the middle of the text. Seeing this I immediately though - I can do this in ColdFusion! It turned out to be rather easy. First let's start with a new quote:

<cfsavecontent variable="quote"> So here I was, home again after all those years. Standing in the main square (which I had crossed countless times as a child, as a boy, as a young man), I felt no emotion whatsoever; all I could think was that the flat space, with the spire of the town hall (like a soldier in an ancient helmet) rising above the rooftops, looked like a huge parade ground and that the military past of the Moravian town, once a bastion against Magvar and Turk invaders, had engraded an irrevocable ugliness on it's face. </cfsavecontent>

(Taken from the first page of "The Joke".) What I figured I'd do is grab a fullLeft from, well, the left, and do a FullRight for the end of the text. There is no FullRight, so what I tried instead was:

<cfset test = reverse(fullLeft(reverse(quote), 200))>

Which worked like a charm! I basically reversed the text, did a fullLeft, and reversed the result. The complete code example (minus the cfsavecontent) is below:

<cfoutput> #fullleft(quote, 200)#<br /> ... <br /> </cfoutput> <cfset test = reverse(fullLeft(reverse(quote), 200))> <cfoutput>#test#</cfoutput>

The final result:

So here I was, home again after all those years. Standing in the main square (which I had crossed countless times as a child, as a boy, as a young man), I felt no emotion whatsoever; all I could
...
the rooftops, looked like a huge parade ground and that the military past of the Moravian town, once a bastion against Magvar and Turk invaders, had engraded an irrevocable ugliness on it's face.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Emmet posted on 5/30/2008 at 2:23 AM

I just finished reading the first and the last page of the bible. I fully left out the middle.

Comment 2 by Scott Bennett posted on 5/30/2008 at 3:04 AM

@Emmet,

If my memory serves me correctly that would be something like "In the beginning... Amen."

It reminds me of that Seinfeld Episode with all the "Yadda Yadda Yadda"

@Ray,
You should make a UDF that does this and call it YaddaYaddaYadda() =)

Comment 3 by Matthew posted on 5/30/2008 at 3:46 AM

There is 1 problem though. If your original text string is less than 400 than you'd have some repetition. Perhaps the 200 number needs to be dynamically calculated:
#fullleft(quote, IIf(Len(myString)<400,DE(Len(myString)*0.4),DE('200')))#
This will get 40% of the string if the string is less than 400.

Comment 4 by Raymond Camden posted on 5/30/2008 at 4:50 AM

@Matthew - I actually cover that in the earlier blog entry. I shoulda mentioned it here again though.

Comment 5 by Matthew posted on 5/30/2008 at 4:57 AM

No problems Raymond. I was just being nit picky really. Thanks for the post by the way!
Cheers
Matthew

Comment 6 by Raymond Camden posted on 5/30/2008 at 4:59 AM

Heh, I've been known to be a bit picky myself. ;)

Comment 7 by Joshua Curtiss posted on 5/31/2008 at 12:04 AM

Adding to the pickiness... :-)

Usually when adding the elipsis in the middle, you make sure the sentence being read still makes sense even though the stuff in the middle is cut out. Since this obviously can't be done with programming, the left-only truncation would obviously be a better bet!