Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Triming specific number of words before &
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Triming specific number of words before &

 Post Reply Post Reply Page  123>
Author
usaboy View Drop Down
Newbie
Newbie
Avatar

Joined: 05 July 2003
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote usaboy Quote  Post ReplyReply Direct Link To This Post Topic: Triming specific number of words before &
    Posted: 05 July 2003 at 11:02am
Hi Experts..  i have designed a search page.. and in the result page i have the string matching the query and the words found in the string are highlighted using a function..

the thing that I want to do now is to trim the string to 10 words before the matching keywords and 10 words after the matching keywords..  and a link to detail page so they can view the full text matching the query.. i don't have probelm with detail page. but with triming.. i just don't know what to do... i need a function to do that for me.. it will be a great help if anyone can give me the function to the triming as follow :

        trim 10 words before the matching keywords and 10 words after the matching keywords in a string

tanx sooo much.
Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post Posted: 05 July 2003 at 11:23am
so you want to do something like...

Stuff on page: Hello world my name is paul and i really like computers. I just finished cutting the grass, its is really hot where i live...

you input "finished"

your want outputed: ...name is paul and i really like computers. I just finished cutting the grass, its is really hot where i live... 
Back to Top
usaboy View Drop Down
Newbie
Newbie
Avatar

Joined: 05 July 2003
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote usaboy Quote  Post ReplyReply Direct Link To This Post Posted: 05 July 2003 at 1:49pm

Hi Paul... yes.. that's EXACTLY what I want to do in my ASP (VBscript) page.



Edited by usaboy
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: 05 July 2003 at 4:33pm

I think its inStr which returns the position of the keyword, IE

strKeyword = "finished"
intCharacterPositionOfKeyWord = inStr(strEntireLine,strKeyword)

Might return for example 50.   I think the easiest way then would be to do a simple loop, going through each character of the sentence one way then the other, and everytime it comes accross a space bar it adds one to the counter, then the loop terminates when its hit 10 spaces, if that makes sense!  Coz im bored and feeling nice ill do an example:


strKeyword = "finished"
strSentence = "Hello world my name is paul and i really like computers. I just finished cutting the grass, its is really hot where i live"

intPositionToStartFrom = inStr(strEntireLine,strKeyword)

If intPositionToStartFrom <> 0 then

  intCharLoopCount = intPositionToStartFrom

  'Get the right 10 words
  Do until intWordcount = 10
    If mid(strEntireLine,intCharLoopCount,1) = " " then
      intWordCount = intWordCount + 1
    end if
    strToTheRight10Words = strToTheRight10Words + (mid(strEntireLine,intCharLoopCount,1))
    intCharLoopCount = intCharLoopCount + 1
  Loop

  intCharLoopCount = intPositionToStartFrom

  'Get the left 10 words
  Do until intWordcount = 10
    If mid(strEntireLine,intCharLoopCount,1) = " " then
      intWordCount = intWordCount + 1
    end if
    strToTheLeft10Words = strToTheLeft10Words + (mid(strEntireLine,intCharLoopCount,1))
    intCharLoopCount = intCharLoopCount - 1
  Loop

end if

of course this is really buggy I guess, havent got enough time to check it but should help you along



Edited by Gullanian
Back to Top
usaboy View Drop Down
Newbie
Newbie
Avatar

Joined: 05 July 2003
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote usaboy Quote  Post ReplyReply Direct Link To This Post Posted: 06 July 2003 at 7:06am
hmmm tanx Gullanian.. do u think we need to reset the intWordcount to 0 before starting the second loop??? btw... this didn't work for me :( i really need help on doing this.. I hope Paul gives some tips on that...
Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post Posted: 06 July 2003 at 9:59am

many search engines use trimming scripts, did you try contacting them and asking them how they did it? Some search engine is bound to give you some help...

Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post Posted: 06 July 2003 at 10:00am
i'm going on vacation in two hours... good luck!
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 06 July 2003 at 10:49am
The way I would do this is to limit it by characters not words. Set it to like 100 before the word and 100 character from the beginning of the word.

function FocusKeyword (strSting, strKeyword)
dim intPosition, intLength
intLength = 100
' get keyword's position in the string
intPosition = inStr(strString, strKeyword)

'truncate characters before keyowrd to intLength
if intPosition > intLength then
     strString = Right(strString, len(strString)-intLength)
     intPosition = inStr(strString, strKeyword)
end if

' truncate characters after the beginning of the keyword to intLength
if len(strString) > intPosition + intLength then
     strString = Left(strString, intPosition+intLength)
end if

'truncate to the first space in the string
strString = right(strString, len(strString)-Instr(strString," "))

'truncate to the first space and the end of the string
strString = left(strString, inStrRev(strString))

'highlight keyword
strString = Replace(strString, strKeyword, "<b>"& strKeyword &"</b>")
'return string
FocusKeyword = strString
end function

I didn't test the function so it may not be perfect, but it will give to the general idea.
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.