Solved

Limit Words

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Redirect website ! 4 52
file too large for website 6 67
.htaccess rewrite url with querystring problem 13 85
Problem to go to page 12 81
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

12 Experts available now in Live!

Get 1:1 Help Now