Send me more information about:

ASP Script, Get first paragraph

Function to return the first paragraph

Many websites use summaries to list out articles and news items, frequently chopping off the first sentence or offering the non too attractive ... trailing characters. Not to be outdone we were asked to display the first paragraph from a recordset. The following function offers this feature:

 

VBscript Function

<%
Function GetFirstPara(str)

Dim NewLen, strLen

If InStr(1, str, "</p>") > 0 Then
NewLen = InStr(1, str, "</p>")
NewLen = Cint(NewLen-1)
Else
NewLen = 150
End if

'Create a File System Object

'Return the file's line count
GetFirstPara = Left(str,NewLen)

End Function

%>

 

Deploying the function

 

<%=GetFirstPara(rs("fieldname"))%>

 

Explanation.

As we use a content editor from an HTML enabled text area (commonly found in CMS systems) many clients use HTML paragraphs thus generating opening and closing <p> tags. But finding the occurance of the first paragraph tag we are able to trim back the first paragraph and write it out. Easy huh?

 

Mark Lawson August 2007