|
|
|
|
Crop the First Sentence
Using the Instr function we can check to find if there
is a full stop in our string and if there is then find it's position.
Next using the Left function we can return a specified number of characters
from the left including the first full stop.
Below is sample code that will retrieve the headline and first sentence
of the news story for each news article in our database 'News.mdb'.
The fields in our database are an 'ID' autonumber, 'headline' which
is a text field and 'news_story' which is a memo field.
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Crop the first sentence</title>
</head>
<body>
<%
Dim oConnection, oRecordset, iFirst
Dim sSQL, sConnString, sNewsTitle, sNewsBody
sSQL="SELECT * FROM tblNews"
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("News.mdb")
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")
oConnection.Open(sConnString)
oRecordset.Open sSQL, oConnection
If oRecordset.EOF Then
Response.Write("There are no news articles.")
Else
Do While Not oRecordset.EOF
sNewsTitle= oRecordset("headline")
sNewsBody= oRecordset("news_story")
If InStr(1, sNewsBody, ".") > 0 Then
iFirst=InStr(1,sNewsBody,".")
sNewsBody=Left(sNewsBody,iFirst)
End If
Response.write "<p>"
Response.Write sNewsTitle & "<br>"
Response.Write sNewsBody
Response.Write "</p>"
oRecordset.MoveNext
Loop
End If
oRecordset.Close
oConnection.Close
Set oRecordset = Nothing
Set oConnection = Nothing
%>
</body>
</html>
Read more on the Instr Function
Read more on the Left Function
If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|
|
|
|
|