GetWords(str, words)
Last updated July 30, 2001
Version: 2 | Requires: CF5 | Library: StrLib
Description:
Supply a string and number of words to return, this returns that number of words with an ellipsis at the end. If the number of words argument is greater than the actual number of words, the original string will be returned without the ellipsis.
Return Values:
Example:
<cfset sentance = "Hello, this is a sentance with many words and spaces.">
<cfoutput>#getWords(sentance,4)#</cfoutput>
Parameters:
Name | Description | Required |
---|---|---|
str | The string to modify. | Yes |
words | The number of words to display. | Yes |
Full UDF Source:
/**
* Shortens a string without cutting words in half.
* Modified by Raymond Camden on July 30, 2001
*
* @param str The string to modify.
* @param words The number of words to display.
* @author David Grant (david@insite.net)
* @version 2, July 30, 2001
*/
function getWords(str,words) {
var numWords = 0;
var oldPos = 1;
var i = 1;
var strPos = 0;
str = trim(str);
str = REReplace(str,"[[:space:]]{2,}"," ","ALL");
numWords = listLen(str," ");
if (words gte numWords) return str;
for (i = 1; i lte words; i=i+1) {
strPos = find(" ",str,oldPos);
oldPos = strPos + 1;
}
if (len(str) lte strPos) return left(str,strPos-1);
return left(str,strPos-1) & "...";
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016