473,412 Members | 5,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

String question: Returning portion of string with words surrounding highlighted search term?

I'm looking to find or create an ASP script that will take a string, examine
it for a search term, and if it finds the search term in the string, return
the highlighted search term along with the words that surround it. In other
words, I want the search term highlighted and shown in an excerpt of the
context in which it appears.

Any suggestions or pointers? This behavior is most often seen as part of a
search engine. In my case, I want to use it as part of a content "scanner"
that utilizes a screen scraping component.

Jul 19 '05 #1
4 2658
The hard part of this problem isn't highlighting the search term, which has
been done a million times before, but rather pulling out, say, 300
characters that precede the search term and 300 characters that follow it,
so that I can show the context that a term appeared in.

"Mike" <so***@sorry.com> wrote in message
news:bh**********@geraldo.cc.utexas.edu...
"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
I'm looking to find or create an ASP script that will take a string, examine
it for a search term, and if it finds the search term in the string,

return
the highlighted search term along with the words that surround it. In

other
words, I want the search term highlighted and shown in an excerpt of the
context in which it appears.

Any suggestions or pointers? This behavior is most often seen as part of a search engine. In my case, I want to use it as part of a content "scanner" that utilizes a screen scraping component.


Sounds like a job for replace...

replace(string, search-string, "<span style=""background-color:

#FF0000"">" & search-string & "</span>")

I haven't tried it, but it seems logical. The CSS might be flaky above, you might want to verify that background-color is a valid property. :)

--
Mike

Jul 19 '05 #2
This will get you started:

Dim Pos
Dim Extract
Const BufferLen = 300

Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)

If (Pos<>0) Then

' Search term found.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))

End If

It'll need some tweaking and I haven't tested it. Make sure you don't read
past either end of SourceString. I'd be a bit worried about the efficiency
of this if you're doing it multiple times on a page - you'll have to
optimise it by storing the Pos of the last hit and starting from that
position for your next iteration. Perhpas something like this (untested):

Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)
Do While Pos <> 0

' Get the extract.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))
' Next Pos.
Pos = InStr (Pos, SourceString, SearchTerm, vbTextCompare)

Loop

Try that.

Alan

"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
The hard part of this problem isn't highlighting the search term, which has been done a million times before, but rather pulling out, say, 300
characters that precede the search term and 300 characters that follow it,
so that I can show the context that a term appeared in.

"Mike" <so***@sorry.com> wrote in message
news:bh**********@geraldo.cc.utexas.edu...
"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
I'm looking to find or create an ASP script that will take a string, examine
it for a search term, and if it finds the search term in the string,

return
the highlighted search term along with the words that surround it. In

other
words, I want the search term highlighted and shown in an excerpt of the context in which it appears.

Any suggestions or pointers? This behavior is most often seen as part
of a search engine. In my case, I want to use it as part of a content "scanner" that utilizes a screen scraping component.


Sounds like a job for replace...

replace(string, search-string, "<span style=""background-color:

#FF0000"">"
& search-string & "</span>")

I haven't tried it, but it seems logical. The CSS might be flaky above,

you
might want to verify that background-color is a valid property. :)

--
Mike


Jul 19 '05 #3
Yep sorry about that. Try this line for the second search (notice the 'Pos +
1'):

Pos = InStr (Pos + 1, SourceString, SearchTerm, vbTextCompare)

See how you go.

Alan

"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
Alan, thanks, this is great. Function #1 works just swell, with no mishaps
at all. I combined it with a "clean out the HTML" function and a "highlight search term" function to make a genuinely useful scanner.

The only part I don't have working is a mechanism by which it will loop
through the remaining content. Function #2 appears to send the server into
an infinite loop and I can't figure out why. It would be really great if I could list out all of the "hits" for a given page. Do you have any ideas for what I should be looking at?
"Alan" <SP******************@inspire.net.nz> wrote in message
news:en*************@TK2MSFTNGP12.phx.gbl...
This will get you started:

Dim Pos
Dim Extract
Const BufferLen = 300

Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)

If (Pos<>0) Then

' Search term found.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))

End If

It'll need some tweaking and I haven't tested it. Make sure you don't read
past either end of SourceString. I'd be a bit worried about the efficiency of this if you're doing it multiple times on a page - you'll have to
optimise it by storing the Pos of the last hit and starting from that
position for your next iteration. Perhpas something like this (untested):
Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)
Do While Pos <> 0

' Get the extract.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))
' Next Pos.
Pos = InStr (Pos, SourceString, SearchTerm, vbTextCompare)

Loop

Try that.

Alan

"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
The hard part of this problem isn't highlighting the search term, which
has
been done a million times before, but rather pulling out, say, 300
characters that precede the search term and 300 characters that follow it, so that I can show the context that a term appeared in.

"Mike" <so***@sorry.com> wrote in message
news:bh**********@geraldo.cc.utexas.edu...
> "Ken Fine" <ke*****@u.washington.edu> wrote in message
> news:bh***********@nntp6.u.washington.edu...
> > I'm looking to find or create an ASP script that will take a
string, > examine
> > it for a search term, and if it finds the search term in the string, > return
> > the highlighted search term along with the words that surround it.

In > other
> > words, I want the search term highlighted and shown in an excerpt
of the
> > context in which it appears.
> >
> > Any suggestions or pointers? This behavior is most often seen as

part
of
a
> > search engine. In my case, I want to use it as part of a content
"scanner"
> > that utilizes a screen scraping component.
>
> Sounds like a job for replace...
>
> replace(string, search-string, "<span style=""background-color:
#FF0000"">"
> & search-string & "</span>")
>
> I haven't tried it, but it seems logical. The CSS might be flaky

above, you
> might want to verify that background-color is a valid property. :)
>
> --
> Mike
>
>



Jul 19 '05 #4
Thanks again, Alan, that did the trick! This is great. The finished code
looks like this:

<%Dim Pos
Dim Extract
Const BufferLen = 300
Searchterm=TearSearchTerm
SourceString=strRetVal5

Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)

Do While Pos <> 0

' Get the extract.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))
Response.Write "<p><font face=arial size=2><b>Location of search term
"&"<font color=red>&quot;"&Searchterm&"&quot;</font> at <a
href="&TearURL&">"&TearURL&"</a>"&":</b> char #"& Pos & "</font><br>"
FinishString=StripHTML(Extract)

Response.Write "<b>Excerpt:</b><br><font face=arial size=2> ..."&
doHighlight(FinishString, Searchterm,"hi")&" ...</font>"
' Next Pos.
Pos = InStr (Pos + 1, SourceString, SearchTerm, vbTextCompare)

Loop
%>

Interested readers can sub out the response.write business, and sub in their
own "highlight search word" function that drives the "doHighlight" widget.
(If you want this function, it's available off of planet-source-code.com; I
wrote in an extra setting to the function -- "hi" -- that highlights search
terms with a yellow background.)

StripHTML is a function to remove HTML/code from a string, you can find any
of a number of free functions that will do this for you.

A big gold star to Alan for his timely help on a not-trivial problem.

-KF
"Alan" <SP******************@inspire.net.nz> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Yep sorry about that. Try this line for the second search (notice the 'Pos + 1'):

Pos = InStr (Pos + 1, SourceString, SearchTerm, vbTextCompare)

See how you go.

Alan

"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
Alan, thanks, this is great. Function #1 works just swell, with no mishaps
at all. I combined it with a "clean out the HTML" function and a "highlight
search term" function to make a genuinely useful scanner.

The only part I don't have working is a mechanism by which it will loop
through the remaining content. Function #2 appears to send the server into an infinite loop and I can't figure out why. It would be really great if I
could list out all of the "hits" for a given page. Do you have any ideas for
what I should be looking at?
"Alan" <SP******************@inspire.net.nz> wrote in message
news:en*************@TK2MSFTNGP12.phx.gbl...
This will get you started:

Dim Pos
Dim Extract
Const BufferLen = 300

Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)

If (Pos<>0) Then

' Search term found.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))

End If

It'll need some tweaking and I haven't tested it. Make sure you don't

read past either end of SourceString. I'd be a bit worried about the efficiency of this if you're doing it multiple times on a page - you'll have to
optimise it by storing the Pos of the last hit and starting from that
position for your next iteration. Perhpas something like this (untested):
Pos = InStr (1, SourceString, SearchTerm, vbTextCompare)
Do While Pos <> 0

' Get the extract.
Extract = Mid(SourceString, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm)))
' Next Pos.
Pos = InStr (Pos, SourceString, SearchTerm, vbTextCompare)

Loop

Try that.

Alan

"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:bh***********@nntp6.u.washington.edu...
> The hard part of this problem isn't highlighting the search term, which has
> been done a million times before, but rather pulling out, say, 300
> characters that precede the search term and 300 characters that
follow it,
> so that I can show the context that a term appeared in.
>
> "Mike" <so***@sorry.com> wrote in message
> news:bh**********@geraldo.cc.utexas.edu...
> > "Ken Fine" <ke*****@u.washington.edu> wrote in message
> > news:bh***********@nntp6.u.washington.edu...
> > > I'm looking to find or create an ASP script that will take a string, > > examine
> > > it for a search term, and if it finds the search term in the string, > > return
> > > the highlighted search term along with the words that surround
it. In
> > other
> > > words, I want the search term highlighted and shown in an

excerpt of the
> > > context in which it appears.
> > >
> > > Any suggestions or pointers? This behavior is most often seen as

part
of
> a
> > > search engine. In my case, I want to use it as part of a content
> "scanner"
> > > that utilizes a screen scraping component.
> >
> > Sounds like a job for replace...
> >
> > replace(string, search-string, "<span style=""background-color:
> #FF0000"">"
> > & search-string & "</span>")
> >
> > I haven't tried it, but it seems logical. The CSS might be flaky

above,
> you
> > might want to verify that background-color is a valid property. :)
> >
> > --
> > Mike
> >
> >
>
>



Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: flam | last post by:
Hello, I am having a hard time spliting a string into an array for use in a search. Here is the situation. The user will input a search string. Normally I can just split the string by "split...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
12
by: Ron | last post by:
I am getting an error Option strict on disallows implicit conversion from string to long I get it for this code iStartPosition = InStr(iStartPosition + 1, RichTextBox1.Text, "A" Or "a" the...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.