登录

用户名:

密码:
abbreviate(string, len)     
作者:luckzy1228
发部日期:Mon, 04/16/2007

字符串过长,自动加"..." 

<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>

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

最后更新:Mon, 04/16/2007