Forum Moderators: open

Message Too Old, No Replies

How do I display only a set number of words from a paragraph?

         

Headed North

7:22 pm on Oct 29, 2005 (gmt 0)

10+ Year Member



I need to display a certain number of words from a paragraph on a homepage with a link to the rest of the article. The news text is being pulled from the database and is usually many paragraphs long. I'd like to display only the first 25 words in the first paragraph. Does anyone know of an easy way to parse the news text from the first character and display only the first 25 words?

carguy84

5:07 am on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dim teaser, index, strText
strText = "This is your article. This is your article. This is your article. This is your article. This is your article. This is your article. This is your article. "
teaser = Left( Trim( Replace( Replace( Replace( RemoveHTML( strText ), vbCr, "" ), VbLf, "" ), vbTab, "" ) ), 200 )
index = InstrRev( teaser, " " )
teaser = Left( teaser, index ) & " ..."

aspdaddy

8:49 am on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To guarantee 25 you need to count words not characters, something like this.

Dim teaser, index, strText
strText = "This is your article. This is your article. This is your article. This is your article. This is your article. This is your article. This is your article. "

strText=Replace(strText," "," ")

for index =1 to len(strText)
if mid(strText,index,1)= " " then
words = words+1
end if

if words=24 then
teaser = left(strText,index)
end if

next
Response.write teaser

Headed North

2:50 pm on Oct 30, 2005 (gmt 0)

10+ Year Member



Thank you carguy and aspdaddy. I was able to get it to work perfectly thanks to your help. Really appreciate it!