Professional Web Applications Themes

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

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....

  1. #1

    Default 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.



    Ken Fine Guest

  2. #2

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

    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" <sorrysorry.com> wrote in message
    news:bhe0u8$s7k$1geraldo.cc.utexas.edu...
    > "Ken Fine" <kenfineu.washington.edu> wrote in message
    > news:bhdodq$23m8$1nntp6.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
    >
    >

    Ken Fine Guest

  3. #3

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

    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" <kenfineu.washington.edu> wrote in message
    news:bhe34e$1rea$1nntp6.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" <sorrysorry.com> wrote in message
    > news:bhe0u8$s7k$1geraldo.cc.utexas.edu...
    > > "Ken Fine" <kenfineu.washington.edu> wrote in message
    > > news:bhdodq$23m8$1nntp6.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
    > >
    > >
    >
    >

    Alan Guest

  4. #4

    Default Re: Almost there... Re: String question: Returning portion of string with words surrounding highlighted search term?

    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" <kenfineu.washington.edu> wrote in message
    news:bheotu$2ssa$1nntp6.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" <SPAMMENOTalan.howardinspire.net.nz> wrote in message
    > news:enuiLSfYDHA.384TK2MSFTNGP12.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" <kenfineu.washington.edu> wrote in message
    > > news:bhe34e$1rea$1nntp6.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" <sorrysorry.com> wrote in message
    > > > news:bhe0u8$s7k$1geraldo.cc.utexas.edu...
    > > > > "Ken Fine" <kenfineu.washington.edu> wrote in message
    > > > > news:bhdodq$23m8$1nntp6.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
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Alan Guest

  5. #5

    Default Re: Almost there... Re: String question: Returning portion of string with words surrounding highlighted search term?

    Thanks again, Alan, that did the trick! This is great. The finished code
    looks like this:

    <%Dim Pos
    Dim Extract
    Const BufferLen = 300
    Searchterm=TearchTerm
    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" <SPAMMENOTalan.howardinspire.net.nz> wrote in message
    news:uB5TslgYDHA.3444tk2msftngp13.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" <kenfineu.washington.edu> wrote in message
    > news:bheotu$2ssa$1nntp6.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" <SPAMMENOTalan.howardinspire.net.nz> wrote in message
    > > news:enuiLSfYDHA.384TK2MSFTNGP12.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" <kenfineu.washington.edu> wrote in message
    > > > news:bhe34e$1rea$1nntp6.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" <sorrysorry.com> wrote in message
    > > > > news:bhe0u8$s7k$1geraldo.cc.utexas.edu...
    > > > > > "Ken Fine" <kenfineu.washington.edu> wrote in message
    > > > > > news:bhdodq$23m8$1nntp6.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
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Ken Fine Guest

Similar Threads

  1. Update portion of a string column
    By Yves Glodt in forum IBM DB2
    Replies: 2
    Last Post: September 12th, 06:04 PM
  2. Replies: 5
    Last Post: September 4th, 02:03 AM
  3. Replace words into a string with a space before or after
    By Francesco Moi in forum PERL Miscellaneous
    Replies: 3
    Last Post: August 20th, 01:04 PM
  4. substitute three random words in a string
    By Purl Gurl in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 13th, 08:44 PM
  5. How to pick out words from a non-delimited string?
    By Julian Hsiao in forum PERL Miscellaneous
    Replies: 6
    Last Post: July 29th, 11:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139