Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bobi 346 posts 950 karma points
    Apr 26, 2017 @ 04:25
    Bobi
    0

    Cleansing search term for lucene so it doesn't break

    Does anyone know how to cleanse a search term so that special characters like colons (:), etc. do not break a site search?

    I have:

    // Cleanse the search term
    public string CleanseSearchTerm(string input)
    {
        return Umbraco.StripHtml(input).ToString();
    }
    

    Thanks.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Apr 27, 2017 @ 13:39
    Dennis Adolfi
    100

    Hi Bobi.

    So you want the search to only be able to search for letters or digits? There is probably 100 ways of solving this, but one way is with Regex.

     // Cleanse the search term
            public string CleanseSearchTerm(string input)
            {
                System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9 -]");
                input = rgx.Replace(input, "");
    
                return input.ToString();
            }
    

    This method would transform this string:

    H|jbfsj08 s989w&/#&%#ยค802 95+43223gf:;:`!
    

    Into:

    Hjbfsj08 s989w802 9543223gf 
    

    Best of luck!

  • Bobi 346 posts 950 karma points
    Apr 27, 2017 @ 15:51
    Bobi
    1

    Thanks.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Apr 27, 2017 @ 19:30
    Dennis Adolfi
    0

    You're welcome! Glad to help!

    Have a great day Bobi!

Please Sign in or register to post replies

Write your reply to:

Draft