Skip to main content.
home | support | download

Back to List Archive

RE: Bolding search items on indexed page

From: Januski, Ken <kjanuski(at)not-real.phillynews.com>
Date: Fri Nov 16 2001 - 01:13:56 GMT
Mark (and Bill Moseley who also responsded with some good advice),

Thanks for your suggestions below. It took me awhile to  take what you sent
below and wrestle it into cooperating with my web pages. But once I did the
results were both words and phrases bolded and with that nice yellow
highlight background. It works great.

Ken



> -----Original Message-----
> From:	Mark Kennedy [SMTP:MarkK@fool.com]
> Sent:	Monday, November 12, 2001 4:03 PM
> To:	Multiple recipients of list
> Subject:	[SWISH-E] RE: Bolding search items on indexed page
> 
> Ken:
> 
> I ran into a similar need when I implemented SWISHE for a client.
> Unfortunately, their site is behind a firewall, so I can't give you URLs.
> Here's what I did:
> 
> When a page showed up in the search results, I made the link to that page
> point to a perl CGI script (search-display.cgi) with the actual path to
> the
> document in the querystring. In addition, the querystring contained the
> original search terms. The URL looks something like this:
> http://www.yoursite.com/cgi-bin/search-display.cgi?q=california&file=%2Fpa
> th
> %2Fto%2Fdocument.htm
> 
> Where 'q' is the original query and 'file' is the path to the document to
> display.
> 
> Then, I run the body of the document through the HighlightSearchWords
> function below which parses the query into words and phrases, highlighting
> each accordingly. This works great for my application. Hope it helps you
> some!
> 
> Mark Kennedy
> 
> Code sample:
> 
> sub HighlightSearchWords {
>         # $text is the body of the document.
>         # $q is the original query passed to SWISHE.
>         my ($text, $q) = @_;
>         my @phrases = ();
>    
>         # These are words that I don't want to highlight because they are
> so
> common,
>         # or because they are boolean search terms (and, or, not).
>         my @skipwords = qw( to of from for the and but a or not );
> 
>         # The beginning of the highlighting markup.
>         my $highlightStart = "<b
> style='color:black;background-color:#ffff66'>";
> 
>         # The end of the highlighting markup.
>         my $highlightEnd = "</b>";
> 
>         # Find the phrases. They are found between double-quotes in $q.
>         while( $q =~ /"(.*?)"/g ) {
>                 push(@phrases, $1);
>         }
> 
>         # Remove the phrases from the query and highlight them.
>         foreach my $phrase(@phrases) {
>                 $q =~ s/"$phrase"//gi;
>                 # Highlight the phrase.
>                 $text =~ s/($phrase)/$highlightStart$1$highlightEnd/gi;
>         }
> 
> 
>         # Remove the 'skip' words, which are words that we don't want to
> highlight.
>         foreach my $word( @skipwords ) {
>                 $q =~ s/\b$word\b/ /gi;
>         }
> 
>         # Break the remaining query into an array of words.
>         my @words = split(/ /, $q);
> 
>         # Highlight the remaining words.
>         foreach my $word(@words) {
>                 # Make sure it really contains word characters.
>                 if( $word =~ /\w/ ) {
>                         my $re;
>                         # See if the query
>                         if( $word =~ /\*/ ) {
>                                 $re = "\\b($word)";
>                         } else {
>                                 $re = "\\b($word)\\b";
>                         }
>                         $text =~ s/$re/$highlightStart$1$highlightEnd/gi;
>                 }
>         }
>         # Return the resulting parsed $text.
>         return $text;
> }
> 
> 
> -----Original Message-----
> From: Januski, Ken [mailto:kjanuski@phillynews.com]
> Sent: Monday, November 12, 2001 3:38 PM
> To: Multiple recipients of list
> Subject: [SWISH-E] Bolding search items on indexed page
> 
> 
> Hi,
> 
> I've recently used SWISHE to index a huge number of public records
> available
> on the web. I've also written a web page to allow a user to enter a search
> term and then have SWISHE return a list of the pages that include the
> term.
> But what I'd like to do now is bold the search term whenever a user clicks
> on a link to a found page. I think I'd need to do this by editing the
> returned file on the fly before it displays. I of course don't want to
> alter
> the original public document.
> 
> I realize that this isn't specifically SWISHE related but I thought that
> others might have run into a similar situation and wondered what solutions
> they've come up with if any.
> 
> Thanks for any ideas,
> 
> ken
> 
Received on Fri Nov 16 01:14:43 2001