Excerpt Text
- Home
- ASP
- Excerpt Text
I have a memo field full of text and inter-mixed html tags. Just show me a 100 character snippet of the text, strip out all the HTML tags so we just have the raw text only, and if the leftover raw text is more than 100 characters, keep on writing text to the screen until the first period(.), exclamation point(!), or question mark(?) [indicating the end of a sentence].
Now, if there is still more text to display, show the standard "Read More..." at the end of the snippet. You could just as easily hyperlink the "Read More..." to the current record ID or something too, taking the user to the full record with text and HTML tags retained.
<%
Function Excerpt(theString, length)
theString = theString & " "
a = 0
Do
a = InStr(a + 1, theString, "<")
If a > 0 Then
b = InStr(a, theString, ">")
theString = Left(theString, a - 1) & Mid(theString, b + 1)
End If
Loop Until a = 0
a = InStr(length, theString, ". ")
b = InStr(length, theString, "! ")
c = InStr(length, theString, "? ")
If a = 0 Then a = Len(theString)
If b = 0 Then b = Len(theString)
If c = 0 Then c = Len(theString)
If a < b And a < c Then
outputLimit = a
ElseIf b < a And b < c Then
outputLimit = b
ElseIf c < b And c < a Then
outputLimit = c
Else outputLimit = Len(theString)
End If
outputString = Left(theString, outputLimit)
If Len(RTrim(theString)) > Len(RTrim(outputString)) Then more = " <font face=""Verdana, Arial"" size=1>Read More...</font>"
Excerpt = outputString & more
End Function
%>
<html><body bgcolor=White>
<%
strContent = "I will start searching for the next period after the 100th character for multiple characters. That is a <b>bold</b> & amusing statement! You're going to eat supper right? This is my copyright symbol right here: ©. I will start searching for the next period after the 100th character for multiple characters. I will start searching for the next period <font color=""#FF0000"">after</font> the 100th character for multiple characters. I will start searching for the next period after the 100th character for multiple characters."
Response.Write Excerpt(strContent, 100)
%>
</body></html>
excerpt text asp excerp dot dot dot ADO VBScript tutorial authoring programming howto reference examples samples source code demos tips