using

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

#region

 

 

strTagLess = regEx.Replace(strTagLess,

 

ClearHTMLTags
/// <summary>
/// ClearHTMLTags
/// </summary>
/// <param name="strHTML">Html as text (without encoded)</param>
/// <param name="intWorkFlow">
/// 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
/// </param>
/// <returns>Html stripped off text</returns>
/// <remarks>Author: Narendra Tiwari, Date: 06 Feb 2007</remarks>
/// <example>
/// HtmlOperations operations = new HtmlOperations();
/// strFileData = operations.ClearHTMLTags(File.ReadAllText(filePath), 0);
/// </example>
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, "&lt;");
regEx =
new Regex("[>]", RegexOptions.IgnoreCase);//matches a single >
strTagLess = regEx.Replace(strTagLess, "&gt;");
}
//3. return the stripped off text
return strTagLess;
}
catch
{
throw;
}
}

#endregion

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# Links (5/2/2007)

.NET OrderedDictionary: A generic implementation of IOrderedDictionary GhostDoc 2.0.0 Released Check 5/3/2007 7:15 AM | Member Blogs

# re: Remove HTML Tags from HTML string

public static string ClearHTMLTags(string source)
{
if (string.IsNullOrEmpty(source))
return source;
string temp = source;
while (temp.IndexOf('<') != -1 && temp.IndexOf('>') != -1)
{
int start = temp.IndexOf('<');
int end = temp.IndexOf('>');
temp = temp.Remove(start, end - start + 1);
}
return temp;
} 4/24/2008 1:44 PM | JerManS

# re: Remove HTML Tags from HTML string

zxczx aas 6/26/2008 5:45 PM | fdsf

# re: Remove HTML Tags from HTML string

zxczx aasgfdgdfg 6/26/2008 5:45 PM | fdsf

# re: Remove HTML Tags from HTML string

To remove particular html tag see
http://urenjoy.blogspot.com/2008/10/remove-html-tags-from-string.html 10/21/2008 7:02 PM | Dev

# re: Remove HTML Tags from HTML string

I use this function:
public string Strip(string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
} 5/13/2009 9:59 AM | Hung Na

# re: Remove HTML Tags from HTML string

That was an inspiring post,

I totlay got what you said in the article but when do we need to remove tag from the HTML element can you please tell us what is the possible us of that ?


Thanks for bringing this up 8/19/2009 9:52 AM | web development company

# re: Remove HTML Tags from HTML string

public string Strip(string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
} is the best 8/24/2009 12:12 AM | timber

# re: Remove HTML Tags from HTML string

This works well:

public static string ClearHTMLTags(string source)
{
if (string.IsNullOrEmpty(source))
return source;
string temp = source;
while (temp.IndexOf('<') != -1 && temp.IndexOf('>') != -1)
{
int start = temp.IndexOf('<');
int end = temp.IndexOf('>');
temp = temp.Remove(start, end - start + 1);
}
return temp;
} 9/13/2009 3:37 PM | HasanG

# re: Remove HTML Tags from HTML string

Thanks Naren. Its very useful to me 11/10/2009 11:29 AM | balaramesh

# re: Remove HTML Tags from HTML string

hi,
I had string like "good article <a href='http://'"
This is removed by Regex.Replace(@"[<*]",""); 11/10/2009 11:32 AM | balaramesh

# re: Remove HTML Tags from HTML string

i used this function and run well.
<%
Function RemoveHTML( strText )
Dim nPos1
Dim nPos2

nPos1 = InStr(strText, "<")
Do While nPos1 > 0
nPos2 = InStr(nPos1 + 1, strText, ">")
If nPos2 > 0 Then
strText = Left(strText, nPos1 - 1) & Mid(strText, nPos2 + 1)
Else
Exit Do
End If
nPos1 = InStr(strText, "<")
Loop

RemoveHTML = strText
End Function
%> 5/26/2010 12:15 AM | kürtçe müzik dinle

# re: Remove HTML Tags from HTML string

fdsg s <br/> 12/27/2011 8:56 PM | sfgss

Post a comment





 

 

News

Employers
Soppa Group India
iSmart Panache Inc
R Systems Internationals Ltd
Technovate eSolutions Pvt Ltd
The contents of this blog are my personal opinion and do not represent in any way the view of my employer.
These postings are provided "AS IS" with no warranties, and confer no rights.

Google PR™ - Post your Page Rank with MyGooglePageRank.com

Archives

Post Categories

Image Galleries

Articles & Magazines

ASP.Net 2.0 Compilation

ASP.Net, Blogs I refer...

Atlas

Dost

Drivers and Software Download

Garhwal

Travel Domain

WSS and WebParts

Syndication: