Triming specific number of words before &
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=4082
Printed Date: 30 March 2026 at 10:06am Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com
Topic: Triming specific number of words before &
Posted By: usaboy
Subject: Triming specific number of words before &
Date 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.
|
Replies:
Posted By: pmormr
Date 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...
------------- Paul A Morgan
http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/
|
Posted By: usaboy
Date Posted: 05 July 2003 at 1:49pm
|
Hi Paul... yes.. that's EXACTLY what I want to do in my ASP (VBscript) page.
|
Posted By: Gullanian
Date 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
|
Posted By: usaboy
Date 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...
|
Posted By: pmormr
Date 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...
------------- Paul A Morgan
http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/
|
Posted By: pmormr
Date Posted: 06 July 2003 at 10:00am
i'm going on vacation in two hours... good luck!
------------- Paul A Morgan
http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/
|
Posted By: ljamal
Date 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.
------------- L. Jamal Walton
http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming
|
Posted By: Gullanian
Date Posted: 06 July 2003 at 11:37am
|
only thing is the snippet might start half way through a word, it would look better if you got x words before and after.
Yeah the thing I wrote should give you an idea of how to do it, but it is very buggy i didnt test it just wrote it in 5 mins
|
Posted By: usaboy
Date Posted: 06 July 2003 at 12:10pm
|
tanx ljamal... i did try to use that function but there were no result and i was getting the following error :
Technical Information (for support personnel)
- Error Type:
Microsoft VBScript runtime (0x800A01C2) Wrong number of arguments or invalid property assignment: 'inStrRev' /website4/SearchResults.asp, line 330
|
Posted By: ljamal
Date Posted: 06 July 2003 at 12:42pm
Change :
strString = left(strString, inStrRev(strString))
to
strString = left(strString, inStrRev(strString," "))
------------- L. Jamal Walton
http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming
|
Posted By: usaboy
Date Posted: 06 July 2003 at 1:41pm
|
tanx ljamal... now i get the result page with no error.. but the problem is that there's nothing shown on the page.. just the ... trailing words before and after and the repeated region cols..
i guess somehow the whole string iss truncted!!! any idea/?
|
Posted By: ljamal
Date Posted: 06 July 2003 at 3:26pm
function FocusKeyword (strSting, strKeyword)
should be
function FocusKeyword (strString, strKeyword)
I wrote this function when I read your post, so I didn't check it.
Here I even tested it for you this time so it does work. I found that 100 returned a string too long for my liking so I changed 100 to 50...
http://69.26.135.138/test.asp - see it in action
------------- L. Jamal Walton
http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming
|
Posted By: ljamal
Date Posted: 06 July 2003 at 3:26pm
Just posting the completed function:
function FocusKeyword (strString, strKeyword)
dim intPosition, intLength
intLength = 50
' 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
------------- L. Jamal Walton
http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming
|
Posted By: usaboy
Date Posted: 06 July 2003 at 4:20pm
|
tanx sooooo much ljamal... it did work for me too... but now the problem that i have is with multiple keywords search parameters... that this function works well with ONE keyword... but for more than ONE keyword it just shows few words... any idea what we can do about this???
once again i really appreciate your kind help and support.
|
Posted By: ljamal
Date Posted: 06 July 2003 at 4:54pm
The way the search engines deal with this is they find the first instance of any of the search terms (together or separate) and work from there. If there are other search terms near the first word, then they highlight all that appear.
I would use the split to separate the keywords into an array and determine which one appears first in the string. Then I would use that one to determine the length of the string and then highlight all the words in the array that appear in the new string. If I have time later, I'll work on it and post the function back to this thread.
------------- L. Jamal Walton
http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming
|
Posted By: the boss
Date Posted: 08 July 2003 at 2:31pm
|
here is a little code too
Return x number of characters after x number of characters sometext="Welcome to our Web Site!!" Response.write(Mid(sometext, 9, 2))
return characters from right or left sometext="Welcome to our Web Site!!" Response.write(Left(sometext,5)) Response.write("<br>") Response.write(Right(sometext,5))
------------- http://www.web2messenger.com/theboss">
|
|