Remove HTML Tags from HTML string

using

System;
using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;using System.IO;

#region

strTagLess = regEx.Replace(strTagLess, ClearHTMLTags
///


/// ClearHTMLTags///

/// Html as text (without encoded)
///
/// An integer that if equals to 0 runs only the RegExp filter// .. 1 runs only the HTML source render filter
// .. 2 runs both the RegExp and the HTML source render
// .. >2 defaults to 0
///
/// Html stripped off text
/// Author: Narendra Tiwari, Date: 06 Feb 2007
///
/// HtmlOperations operations = new HtmlOperations();/// strFileData = operations.ClearHTMLTags(File.ReadAllText(filePath), 0);///
public string ClearHTMLTags(string strHTML, int intWorkFlow){
Regex regEx = null;
string strTagLess = string.Empty;try
{
strTagLess = strHTML;//1. "remove html tags"

if (intWorkFlow != 1)
{
//this pattern mathces any html tag
regEx = new Regex("<[^>]*>", RegexOptions.IgnoreCase);"");

//all html tags are stripped
}//2. "remove rouge leftovers"// "or, I want to render the source"
// "as html."
//We *might* still have rouge < and >
//let's be positive that those that remain
//are changed into html characters     if (intWorkFlow > 0 && intWorkFlow < 3){
regEx = new Regex("[<]", RegexOptions.IgnoreCase);//matches a single <
strTagLess = regEx.Replace(strTagLess, "<");
regEx = new Regex("[>]", RegexOptions.IgnoreCase);//matches a single >
strTagLess = regEx.Replace(strTagLess, ">");
}//3. return the stripped off text
return strTagLess;
}catch
{
throw;
}
}

#endregion

This article is part of the GWB Archives. Original Author: Narendra Tiwari

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.