views:

1344

answers:

3

Hi all,

I am outputting a list of search results for a given string of keywords, and I want any matching keywords in my search results to be highlighted. Each word should be wrapped in a span or similar. I am looking for an efficient function to do this.

E.g.

Keywords: "lorem ipsum"

Result: "Some text containing lorem and ipsum"

Desired HTML output: "Some text containing <span class="hit">lorem</span> and <span class="hit">ipsum</span>"

My results are case insensitive.

Many thanks, Tim

+1  A: 

try highlighter from Lucene.net

http://incubator.apache.org/lucene.net/docs/2.0/Highlighter.Net/Lucene.Net.Highlight.html

How to use:

http://davidpodhola.blogspot.com/2008/02/how-to-highlight-phrase-on-results-from.html

EDIT: As long as Lucene.net highlighter is not suitable here new link:

http://mhinze.com/archive/search-term-highlighter-httpmodule/

Trickster
Looks good, but do I have to be using Lucene.Net for my search results to use the Lucene highligher functions? I'm actually just using a simple stored procedure (the data is only in one table so I don't want to have to build and maintain a separate Lucene index).
TimS
Here you can find sources https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/C%23/contrib/Highlighter.Net/Highlighter.Net/. Possibly it will help you make a decision
Trickster
Hmm. Look like you can use it only with Lucene. (( But may be you can use some code from this project...
Trickster
+1  A: 

Use the jquery highlight plugin.

For highlighting it at server side

protected override void Render( HtmlTextWriter writer )
{
 StringBuilder html = new StringBuilder();
 HtmlTextWriter w = new HtmlTextWriter( new StringWriter( html ) );

 base.Render( w );

 html.Replace( "lorem", "<span class=\"hit\">lorem</span>" );

 writer.Write( html.ToString() );
}

You can use regular expressions for advanced text replacing.

You can also write the above code in an HttpModule so that it can be re used in other applications.

Hemanshu Bhojak
Thanks for the idea - In this instance I'm trying to do this server side, as it needs to work on a variety of non-JavaScript devices.
TimS
+1  A: 

Here's what I've decided on. An extension function that I can call on the relevant strings within my page / section of my page:

public static string HighlightKeywords(this string input, string keywords)
{
    if (input == string.Empty || keywords == string.Empty)
    {
     return input;
    }

    string[] sKeywords = keywords.Split(' ');
    foreach (string sKeyword in sKeywords)
    {
     try
     {
      input = Regex.Replace(input, sKeyword, string.Format("<span class=\"hit\">{0}</span>", "$0"), RegexOptions.IgnoreCase);
     }
     catch
     {
      //
     }
    }
    return input;
}

Any further suggestions or comments?

Cheers, Tim

TimS
First, this is going to recognise partial matches within words. Your regex needs to be doing whole word replacements only. Secondly, you can enter `' '` instead of `Convert.ToChar(" ")`
Richard
Thanks Richard - good tip for char, I knew there must be a better way but it hadn't clicked. RE partial matches, that's what I'm after in this case, as the search uses wildcards (hence the need to make things clearer with highlighting).
TimS
thank you! this worked well for me.
Geovani Martinez
I'm not sure but there are javascript files for text highlighting. Ex: http://www.eggheadcafe.com/articles/highlight_google_keywords.asp
HasanGursoy