?
Solved

Limit Words

Posted on 2003-11-17
5
Medium Priority
?
211 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 1000 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

Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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