Solved

Limit Words

Posted on 2003-11-17
5
192 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now