Print Page | Close Window

Trimming Words

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=13929
Printed Date: 30 March 2026 at 1:11pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Trimming Words
Posted By: Misty
Subject: Trimming Words
Date Posted: 21 February 2005 at 3:20pm
I have a database field that is a memo type. I would like to be able to only display the first 50 words from the database. I would like for it to show ..... after about 50 words.
 
I am using <%=Content %>. How do I trim this field to show only 50 words and then ....?



Replies:
Posted By: Mart
Date Posted: 21 February 2005 at 3:30pm

Function LengthTrim(input, length)
If Len(input) > length Then
Return Left(input, length) + "..."
Else
Return input
End If
End Function


then <%= LengthTrim(Content, 50) %>

btw I don't guarentee that code will work - haven't coded in ASP for about 2 years


Posted By: Gullanian
Date Posted: 21 February 2005 at 3:31pm
Well a rough estimate to the amount of words in the sentence is by number of white space characters, so search for the position of the 50th whitespace character and cut it from there.


Posted By: Gullanian
Date Posted: 21 February 2005 at 3:32pm
Mart,

Dont mean to sound critical but the function seems to just re-do the job of the left() function:
Function LengthTrim(input, length)
Left(input, length)
Same difference, right?  I think Misty wanted words anyway.

Edit:  No actually you probably would want to use that function if its being called a lot.  My mistake


Posted By: Misty
Date Posted: 21 February 2005 at 4:48pm
I tried the following code:
 
Code:
Function LengthTrim(input, length)
If Len(input) > length Then
Return Left(input, length) + "..."
Else
Return input
End If
End Function
 
I also used <%= LengthTrim(Content, 50) %>
I got the following error message:

Microsoft VBScript runtime error '800a01f4'

Variable is undefined: 'Return'

There must be a typo. Can someone please help me with this?
 
 


Posted By: Gullanian
Date Posted: 21 February 2005 at 5:28pm
Replace return line with

LengthTrim = input



Posted By: Mart
Date Posted: 21 February 2005 at 5:31pm
told you I hadn't used ASP for 2 yearsSmile


Posted By: Misty
Date Posted: 22 February 2005 at 12:11am
I got the following code to work:
 
Function LengthTrim(input, length)
If Len(input) > length Then
LengthTrim = Left(input, length) + "..."
Else
LengthTrim = input
End If
End Function
 
However, I have decided that I would like to trim words after 4 paragraphs. How can I accomplish this? The end tags for paragraphs are </p>. Can someone please help me with this?


Posted By: dj1811
Date Posted: 22 February 2005 at 1:35am

There is probably a prettier way but this will work:

<%
     FUNCTION ParaTrim(input)
     intP = 0
     intL = 0
     FOR count = 1 to LEN(input)
          IF MID(input,count,4)="</p>" THEN
               intP=intP+1
               IF intP=4 THEN
                   intL=count
               END IF
          END IF
     NEXT
     IF intL>0 THEN
          ParaTrim = LEFT(input,intL-1) & "...."
     ELSE
          ParaTrim = input
     END IF
     END FUNCTION
%>
 
<%
     strP=ParaTrim(strP)
     Response.Write strP
%>


Posted By: dpyers
Date Posted: 22 February 2005 at 2:05am
One way...

Option Explicit
Dim strStartPos
Dim strEndPos
Dim strLength
Dim strMaxLength
Dim strFullText
Dim strText
Dim strSearchString
Dim i

Sub Get4Paragraphs(strFullText)
 
 strSearchString = "</p>"
 strStartPos = 0
 strLength = 0
 strEndPos = 1
 
 strMaxLength = Len(strFullText)
 
 
 'In case we don't have 4 paragraphs
 On Error Resume Next 
 For i = 1 to 4
 'Find the end of the Paragraph
  If strEndPos <= strMaxLength then
   strStartPos = strEndPos
   strEndPos = instr(strStartPos, strFullText, strSearchString) + Len(strSearchString)
  End If
 Next
 
 'If there's an error, we probably didn't find 4 pararagraphs,
 'so we'll print everything we did find
 If Err <> 0 Then
  strEndPos = strMaxLength
  Response.write strMaxLength & "Error<br>"
 End if
 
 'Pluck the string out of the Original text
 Response.write mid(strFullText,1,strEndPos -1)
 
End Sub

 
'Mainline Code
 
strFullText = "<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p><p>Paragraph 4</p><p>Paragraph 5</p>"
 
Get4Paragraphs(strFullText)
 


-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Misty
Date Posted: 22 February 2005 at 2:15am
dpyers,
 
Thank you for the code! How do I apply it to <%= Content %>? This is the line of code that I want to apply the function to.
 
 


Posted By: Gullanian
Date Posted: 22 February 2005 at 7:21am
<%=Get4Paragraphs(content)%>


Posted By: Misty
Date Posted: 22 February 2005 at 12:10pm
I used the code that Gullanian gave me. I got the following error message:

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'Get4Paragraphs'

/news.asp, line 358

Can someone please help me to figure this out? I used the code that dpyers gave me except for this line of code: Response.write mid(strFullText,1,strEndPos -1)


Posted By: dj1811
Date Posted: 22 February 2005 at 2:49pm

I can't see a problem with dpyers code that would cause that.  My guess would be that the you are calling the sub, and the sub cannot be found.   Did you put the sub in a seperate file and forget to include it perhaps?  Or forget the <% %> tags surrounding the script?

I am only guessing of course, but it seems to me the sub is not being found.  If you don't hear from dpyers soonish or get an answer on how to correct the problem, this script also works:
 
<%
     FUNCTION ParaTrim(input)
     intP = 0
     intL = 0
     FOR count = 1 to LEN(input)
          IF MID(input,count,4)="</p>" THEN
               intP=intP+1
               IF intP=4 THEN
               intL=count
               END IF
          END IF
     NEXT
     IF intL>0 THEN
          ParaTrim = LEFT(input,intL-1) & "...."
     ELSE
          ParaTrim = input
     END IF
     END FUNCTION
%>
 
You would call it like <% =ParaTrim(content) %>


Posted By: Gullanian
Date Posted: 22 February 2005 at 3:09pm
What is line 358?  The database is probably pulling a null out.


Posted By: Misty
Date Posted: 22 February 2005 at 3:50pm
No, the content field in the databae isn't null.
 
I used the following code:
 
     FUNCTION ParaTrim(input)
    
     Dim intP
     Dim intL
     Dim count
   
     intP = 0
     intL = 0
     FOR count = 1 to LEN(input)
          IF MID(input,count,4)="<p>" THEN
               intP=intP+1
               IF intP=4 THEN
                 intL=count
               END IF
          END IF
     NEXT
     IF intL>0 THEN
          ParaTrim = LEFT(input,intL-1) & "...."
     ELSE
          ParaTrim = input
     END IF
     END FUNCTION
 
I'm not getting an error message anymore. But it is not working right. All of the paragraphs for the first article on http://www.bethedenmbc.org/news.asp - http://www.bethedenmbc.org/news.asp are displayed on the web page. I want only 4 paragraphs and .... to show up. I'm not sure what's wrong. Can someone please help me with this?


Posted By: dj1811
Date Posted: 22 February 2005 at 6:38pm
Originally posted by Misty Misty wrote:

The end tags for paragraphs are </p>.
 
The end tags are not </p>.  That is why the script is not working.  There is a <p> at the beginning of each paragraph only.  Need to search for <p> instead of </p>.  New code in a moment...
 
 
...try this instead:
 
        FUNCTION ParaTrim(input)
     intP = 0
     intL = 0
     FOR count = 1 to LEN(input)
          IF MID(input,count,3)="<p>" THEN
               intP=intP+1
               IF intP=5 THEN
               intL=count
               END IF
          END IF
     NEXT
     IF intL>0 THEN
          ParaTrim = LEFT(input,intL-1) & "...."
     ELSE
          ParaTrim = input
     END IF
     END FUNCTION
 
You can delete the & "...." if you don't like the elipses ....


Posted By: Misty
Date Posted: 25 February 2005 at 1:40am
Thank you all for the help! The code that dj1811 gave me in the last posting worked.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net