Strip all HTML tags from string

This is something we need very often. Either for security reasons or just because of some business rules. I will share a small code snippet with you as an extension method to string type so you can take advantages of using Regular Expressions to remove HTML tags from any user input.

public static string StripHtml(this string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return string.Empty;
    }

    value = Regex.Replace(value, @"<(.|\n)*?>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);

    return value;
}

And this is how you need to use it:

string var = txUserInput.Text;
var = var.StringHtml();

You may also like...

3 Responses

  1. Steffen says:

    Extension-method:

    public static StripMarkup(this string s)
    {
    if (string.IsNullOrEmpty(s))
    {
    return string.Empty;
    }

    s = Regex.Replace(s, @””, string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);

    return s;

    }

    you can now use:

    var myvar = txtUserInput.Text.StripMarkup();

  2. Steffen says:

    apparently it stripped out the regex.. lol.. and I forgot to put in the return-type “string” for the method :) – you need to fix that yourself.

  3. Tim says:

    This worked great. Thanks for this bit of code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.