Solved

Limit Words

Posted on 2003-11-17
5
207 Views
Last Modified: 2013-12-24
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.
0
Comment
Question by:tomkinite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9765858
<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>
0
 
LVL 1

Author Comment

by:tomkinite
ID: 9766447
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.
0
 
LVL 6

Accepted Solution

by:
PE_CF_DEV earned 250 total points
ID: 9766553
Try this I think this will fix the problem :)

<cffunction name="cleanleft" returntype="string" access="public">
<cfargument name="varr" type="string" required="yes">
<cfargument name="length" type="numeric" required="yes">
<cfif length LTE len(varr)>
      <cfset reallength = refind("\b",varr,length)-1>
      <cfset returnvalue=left(varr,reallength)>
      <cfreturn returnvalue>
<cfelse>
      <cfreturn varr>
</cfif>
</cffunction>
0
 
LVL 1

Author Comment

by:tomkinite
ID: 9766607
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.
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9766874
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.
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question