CFLib.org – Common Function Library Project

FormatTeaser(string, number[, urlArgument])

Last updated July 31, 2003

author

Bryan LaPlante

Version: 3 | Requires: CF5 | Library: StrLib

Description:
This function allows you to display n number of characters from a string with out cutting of in the middle of the word. The function will display a link at the end of the string if it is longer than the number of characters desired and the optional url argument is not empty.

Return Values:
Returns a string.

Example:

<cfset mystring = "here is a string with some chars">
<cfoutput>#FormatTeaser(mystring,11,"http://www.yahoo.com")#</cfoutput>

Parameters:

Name Description Required
string String to be modified. Yes
number Number of characters to include in teaser. Yes
urlArgument URL to use for 'more' link. No

Full UDF Source:

/**
 * Displays n number of characters from a string without cutting off in the middle of a word
 * Code used from FullLeft
 * 
 * @param string      String to be modified. (Required)
 * @param number      Number of characters to include in teaser. (Required)
 * @param urlArgument      URL to use for 'more' link. (Optional)
 * @return Returns a string. 
 * @author Bryan LaPlante (blaplante@netwebapps.com) 
 * @version 3, July 31, 2003 
 */
function FormatTeaser(string,number){
    var urlArgument = "";
    var shortString = "";
    
    //return quickly if string is short or no spaces at all
    if(len(string) lte number or not refind("[[:space:]]",string)) return string;
    
    if(arrayLen(arguments) gt 2) urlArgument = "... <a href=""" & arguments[3] & """>[more]</a>";

    //Full Left code (http://www.cflib.org/udf.cfm?ID=329)
    if(reFind("[[:space:]]",mid(string,number+1,1))) {
          shortString = left(string,number);
    } else { 
        if(number-refind("[[:space:]]", reverse(mid(string,1,number)))) shortString = Left(string, (number-refind("[[:space:]]", reverse(mid(string,1,number))))); 
        else shortString = left(str,1);
    }
    
    return shortString & urlArgument;

}

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

Created by Raymond Camden / Design by Justin Johnson