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.
Archived Comments
I just finished reading the first and the last page of the bible. I fully left out the middle.
@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() =)
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.
@Matthew - I actually cover that in the earlier blog entry. I shoulda mentioned it here again though.
No problems Raymond. I was just being nit picky really. Thanks for the post by the way!
Cheers
Matthew
Heh, I've been known to be a bit picky myself. ;)
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!