How to strip/remove HTML in asp.net C#

Checking a user input contains html tag or not is not unfamiliar task to any web development project. Use the following strip html function in c#, you can take the html tag away from user inputs.

public static string StripHtml(string html, bool allowHarmlessTags)
{
      if (html == null || html == string.Empty)
           return string.Empty;

      if (allowHarmlessTags)
           return System.Text.RegularExpressions.Regex.Replace(html, "</?(?i:script|embed|object|frameset|frame|iframe|meta|link|style)(.|\n)*?>", string.Empty);

      return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
}
Advertisement