Home Contact RSS

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();

Steffen said,

March 18, 2009 @ 17:01

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();

Steffen said,

March 18, 2009 @ 17:09

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.

Tim said,

June 2, 2009 @ 21:49

This worked great. Thanks for this bit of code.

RSS feed for comments on this post · TrackBack URI

Leave a Comment