473,554 Members | 3,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2666
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.co m> wrote in message
news:bh******** **@geraldo.cc.u texas.edu...
"Ken Fine" <ke*****@u.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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=""backgro und-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(SourceStrin g, 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(SourceStrin g, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm) ))
' Next Pos.
Pos = InStr (Pos, SourceString, SearchTerm, vbTextCompare)

Loop

Try that.

Alan

"Ken Fine" <ke*****@u.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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.co m> wrote in message
news:bh******** **@geraldo.cc.u texas.edu...
"Ken Fine" <ke*****@u.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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=""backgro und-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.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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******** *****@TK2MSFTNG P12.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(SourceStrin g, 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(SourceStrin g, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm) ))
' Next Pos.
Pos = InStr (Pos, SourceString, SearchTerm, vbTextCompare)

Loop

Try that.

Alan

"Ken Fine" <ke*****@u.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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.co m> wrote in message
news:bh******** **@geraldo.cc.u texas.edu...
> "Ken Fine" <ke*****@u.wash ington.edu> wrote in message
> news:bh******** ***@nntp6.u.was hington.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=""backgro und-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=Tear SearchTerm
SourceString=st rRetVal5

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

Do While Pos <> 0

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

Response.Write "<b>Excerpt :</b><br><font face=arial size=2> ..."&
doHighlight(Fin ishString, 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 "doHighligh t" 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******** ******@tk2msftn gp13.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.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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******** *****@TK2MSFTNG P12.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(SourceStrin g, 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(SourceStrin g, Pos - BufferLen, ((2*BufferLen) +
Len(SearchTerm) ))
' Next Pos.
Pos = InStr (Pos, SourceString, SearchTerm, vbTextCompare)

Loop

Try that.

Alan

"Ken Fine" <ke*****@u.wash ington.edu> wrote in message
news:bh******** ***@nntp6.u.was hington.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.co m> wrote in message
> news:bh******** **@geraldo.cc.u texas.edu...
> > "Ken Fine" <ke*****@u.wash ington.edu> wrote in message
> > news:bh******** ***@nntp6.u.was hington.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=""backgro und-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
5192
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 /\s+/, $string". However, I want to allow the user to put words so they appear and are searched together ie. "search this" would be searched as one...
9
3737
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? Here is an example of just such a usage: <dl class="faq"> <di>
9
6266
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 can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like :...
32
14778
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
2
8545
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 string. I had it working somewhat, but ran into problems if one of the highlighted words could also be part of an HTML tag (such as 'Table' or...
232
13115
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an...
12
1340
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 "A" and "a" are underlined in blue. How can I fix this?
9
2671
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 T. Smith" "Fred Barzowsky" "Department of Oncology" "Office of Student Affairs" "Lewis Hall" etc. etc. etc. I am wondering what the efficient way...
2
2238
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 more code, but all the "checking the root" code would be separated out in the tree class. The node class would be very smooth. I'll try this when...
0
7493
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7775
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7526
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7854
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6113
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5411
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3525
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1107
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.