Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Trimming Words
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Trimming Words

 Post Reply Post Reply Page  <123>
Author
dj1811 View Drop Down
Newbie
Newbie


Joined: 11 February 2005
Status: Offline
Points: 26
Post Options Post Options   Thanks (0) Thanks(0)   Quote dj1811 Quote  Post ReplyReply Direct Link To This Post 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
%>
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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.
 
 
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post Posted: 22 February 2005 at 7:21am
<%=Get4Paragraphs(content)%>
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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)
Back to Top
dj1811 View Drop Down
Newbie
Newbie


Joined: 11 February 2005
Status: Offline
Points: 26
Post Options Post Options   Thanks (0) Thanks(0)   Quote dj1811 Quote  Post ReplyReply Direct Link To This Post 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) %>


Edited by dj1811 - 22 February 2005 at 2:51pm
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post Posted: 22 February 2005 at 3:09pm
What is line 358?  The database is probably pulling a null out.
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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 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?
Back to Top
 Post Reply Post Reply Page  <123>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.