Link to home
Start Free TrialLog in
Avatar of tomkinite
tomkinite

asked on

Limit Words

In CF, to limit characters is no big deal:

#left(query.column,100)#

but what if you wanted to limit characters but code it so it ends the current word.  Example:

With a normal Left() function: "The cat crossed the road, got hit by a car and died.  We took the anim..."
With an enhanced, custom function: "The cat crossed the road, got hit by a car and died. We took the animal..."

Is there a clean way of doing this? Essentially, what it must do is detect a space, and then end the text "preview".


Thanks for any help.
Avatar of PE_CF_DEV
PE_CF_DEV

<cffunction name="cleanleft" returntype="string" access="public">
<cfargument name="varr" type="string" required="yes">
<cfargument name="length" type="numeric" required="yes">
<cfset reallength = refind("\b",varr,length)>
<cfset returnvalue=left(varr,reallength)>
<cfreturn returnvalue>
</cffunction>
Avatar of tomkinite

ASKER

This is great, although:

1. At times, if you put, say, 33 characters for a string length it will sometimes get the first letter of a word.
2. Can we throw a test in that function for the length?  When a length entered exceeds the actual length I get a parameter error.
ASKER CERTIFIED SOLUTION
Avatar of PE_CF_DEV
PE_CF_DEV

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good stuff.  The power of RE.  Is there a good guide for CF-level RE?  I can never find a good one on the net.
I use the stuff from my CF advanced book

http://www.amazon.com/exec/obidos/tg/detail/-/0321127102/qid=1069103235/sr=8-1/ref=sr_8_1/104-2007007-7290338?v=glance&n=507846

Not sure about online tutorials though might through coldfusion regex at google and see what it spits back.