Wednesday, May 06, 2009

 

C# Code Snippet: Strip All HTML Tags

A quick code snippet to strip all HTML tags using a regular expression. If you are going to call something like this often, then it might be an idea to set the RegEx.Options to Compiled.

using System.Text.RegularExpressions;

...

public string StripHTMLTags(string text)

{

return Regex.Replace(text, @"<(?:.\n)*?>", string.Empty);

}

Note: I'm not suggesting using this for a full blown HTML-Sanitiser. See Jeff Atwood's post and the subsequent comments. (Thanks Piers!)

Labels: , , , , ,



    

Powered by Blogger