In the following short article, we are going to build a function which will extract an excerpt from a text string, using classic ASP. The function could be used in blogs, or in general, when we need to display just a part of an article, a news or any long text (maybe providing a link to the complete piece of text).
The function is very simple, but it has a little trick in it: it won't cut a word. Basically we are going to benefit from the InStrRev VBScript function.
Let's see the how we do it.
The functionAs said our excerpt function is very simple. In order to use it, we need to pass it two variables: the text to be cut and the lenght of output.
Here's the code:
The two aforementioned variables are strIn and strLen. The result is strOut.
strOut = trim(strIn)
if strLen < len(strIn) then
strOut = left(strIn,strLen)
if instrrev(strOut," ") then
strOut= left(strOut,instrrev(strOut," "))
excerpt = strOut & "..."
As said we use the InStrRev function to be sure the word is not truncated.
The use and the resultIf we use the function like:
The result will be:
<%=excerpt("This is a very long text", 15)%>
This is a very ...
If we change the lenght to 14 (just to understand the logic of the function), the result will be:
This is a ...
Hope you find the example useful. As usual let me know if you need more help.