CFLib.org – Common Function Library Project

abbreviate(string, len)

Last updated September 6, 2005

Version: 3 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 5 time(s). Average Rating: 4.0

Description:
Similar to the MaxLength UDF, but designed for outputting abbreviated lengths of HTML code (e.g. in list tables). Strips all tags, and makes sure any abbreviation is done to the last space within the given length. Also appends a properly escaped ellipsis character to the returned string.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
#abbreviate("<p>This bit of HTML copy is going to get <em>real</em> chopped up!</p>", 40)#
</cfoutput>

Parameters:

Name Description Required
string String to use. Yes
len Length to use. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Abbreviates a given string to roughly the given length, stripping any tags, making sure the ending doesn't chop a word in two, and adding an ellipsis character at the end.
 * Fix by Patrick McElhaney
 * v3 by Ken Fricklas kenf@accessnet.net, takes care of too many spaces in text.
 * 
 * @param string      String to use. (Required)
 * @param len      Length to use. (Required)
 * @return Returns a string. 
 * @author Gyrus (kenf@accessnet.netgyrus@norlonto.net) 
 * @version 3, September 6, 2005 
 */

function abbreviate(string,len) {
    var newString = REReplace(string, "<[^>]*>"" ""ALL");
    var lastSpace = 0;
    newString = REReplace(newString, " \s*"" ""ALL");
    if (len(newString) gt len) {
        newString = left(newString, len-2);
        lastSpace = find(" "reverse(newString));
        lastSpace = len(newString) - lastSpace;
        newString = left(newString, lastSpace) & "  &##8230;";
    }    
    return newString;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
15 day(s) ago

James Sleeman James Sleeman added
quickSort
25 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
28 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson