Skip to main content.
home | support | download

Back to List Archive

RE: Bolding search items on indexed page

From: Mark Kennedy <MarkK(at)>
Date: Mon Nov 12 2001 - 21:03:02 GMT

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:

Where 'q' is the original query and 'file' is the path to the document to

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

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

        # 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
        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 []
Sent: Monday, November 12, 2001 3:38 PM
To: Multiple recipients of list
Subject: [SWISH-E] Bolding search items on indexed page


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,

Received on Mon Nov 12 21:03:35 2001