0 Replies - 191 Views - Last Post: 30 December 2007 - 07:43 PM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Strip HTML from string using Regular Expressions

Posted 30 December 2007 - 07:43 PM

Description: Pass the string you would like stripped to the method, accept the return value. Need reference to System.Text.RegularExpressions NamespaceThis is a snippet I have used often to strip all HTML characters from a string before putting the data into the database
//namespace reference
using System.Text.RegularExpressions;

/// 
/// method to strip HTML tags using Regular Expressions
/// 
/// String to strip HTML from
/// 
public string StripHTML(string str)
{
    //variable to hold the returned value
    string strippedString;
    try
    {
        //variable to hold our RegularExpression pattern
        string pattern = "<.*?>";
        //replace all HTML tags
        strippedString = Regex.Replace(str, pattern, string.Empty);
    }
    catch
    {
        strippedString = string.Empty;
    }
    return strippedString;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1