Search keyword highlight in ASP.Net

后端 未结 4 425
悲哀的现实
悲哀的现实 2021-02-04 17:23

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

相关标签:
4条回答
  • 2021-02-04 17:36

    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/

    0 讨论(0)
  • 2021-02-04 17:50

    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?

    0 讨论(0)
  • 2021-02-04 17:57

    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.

    0 讨论(0)
  • 2021-02-04 17:58

    An extension to the answer above. (don't have enough reputation to give comment)

    To avoid span from being replaced when search criteria were [span pan an a], the found word was replaced to something else than replace back... not very efficient though...

    public string Highlight(string input)
    {
        if (input == string.Empty || searchQuery == string.Empty)
        {
            return input;
        }
    
        string[] sKeywords = searchQuery.Replace("~",String.Empty).Replace("  "," ").Trim().Split(' ');
        int totalCount = sKeywords.Length + 1;
        string[] sHighlights = new string[totalCount];
        int count = 0;
    
        input = Regex.Replace(input, Regex.Escape(searchQuery.Trim()), string.Format("~{0}~", count), RegexOptions.IgnoreCase);
        sHighlights[count] = string.Format("<span class=\"highlight\">{0}</span>", searchQuery);
        foreach (string sKeyword in sKeywords.OrderByDescending(s => s.Length))
        {
            count++;
            input = Regex.Replace(input, Regex.Escape(sKeyword), string.Format("~{0}~", count), RegexOptions.IgnoreCase);
            sHighlights[count] = string.Format("<span class=\"highlight\">{0}</span>", sKeyword);
        }
    
        for (int i = totalCount - 1; i >= 0; i--)
        {
            input = Regex.Replace(input, "\\~" + i + "\\~", sHighlights[i], RegexOptions.IgnoreCase);
        }
    
        return input;
    }
    
    0 讨论(0)
提交回复
热议问题