ASP Clipping long text and adding elippses
ASP Best Practices
Clip long text to the closest word break to the length desired
Date : 2006-03-10
Many times when dealing with long text fields we will want to display only enough of it to not throw off our layout. We could simply clip the text 3 characters shorter than we want and then add "..." after it but sometimes you end up with some odd looking ends. Wouldn't it be better to able to break the text at the next word break and then add the "..." always leaving a fairly legible sentence?
dim text_trim, iLoc, iLast
sText = sText & " "
if len(sText) => iLength then
iLast = 1
iLoc = 1
if instr(1,sText," ") > 0 then
do while iLoc < iLength AND iLoc > 0
iLast = iLoc+1
iLoc = instr(iLast,sText," ")
text_trim = left(sText,iLast-2) & "..."
text_trim = left(sText,iLength) & "..."
text_trim = sText
clipText = text_trim
This function will try both ways. If there is not a word break (a space) anywhere within the submitted string then it will clip the string at the desired length and add the elippses ("..."). If there are word breaks then it will get as close as possible to the desired length and clip the text at a word break before adding the elippses.
No comments yet