What's Here?
- Members: 300,504
- Replies: 826,160
- Topics: 137,488
- Snippets: 4,420
- Tutorials: 1,148
- Total Online: 1,904
- Members: 90
- Guests: 1,814
|
This is a snippet I have used often to strip all HTML characters from a string before putting the data into the database
|
Submitted By: PsychoCoder
|
|
Rating:

|
|
Views: 7,406 |
Language: C#
|
|
Last Modified: December 31, 2007 |
|
Instructions: Pass the string you would like stripped to the method, accept the return value. Need reference to System.Text.RegularExpressions Namespace |
Snippet
//namespace reference
using System.Text.RegularExpressions;
/// <summary>
/// method to strip HTML tags using Regular Expressions
/// </summary>
/// <param name="str">String to strip HTML from</param>
/// <returns></returns>
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;
}
Copy & Paste
|
|
|
|