Geeks With Blogs

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

Narendra Tiwari

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

Posted on Tuesday, May 1, 2007 11:49 AM .Net | Back to top


Comments on this post: Remove HTML Tags from HTML string

# Links (5/2/2007)
Requesting Gravatar...
.NET OrderedDictionary: A generic implementation of IOrderedDictionary GhostDoc 2.0.0 Released Check
Left by Member Blogs on May 03, 2007 7:15 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
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;
}
Left by JerManS on Apr 24, 2008 1:44 PM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
zxczx aas
Left by fdsf on Jun 26, 2008 5:45 PM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
zxczx aasgfdgdfg
Left by fdsf on Jun 26, 2008 5:45 PM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
To remove particular html tag see
http://urenjoy.blogspot.com/2008/10/remove-html-tags-from-string.html
Left by Dev on Oct 21, 2008 7:02 PM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
I use this function:
public string Strip(string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}
Left by Hung Na on May 13, 2009 9:59 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
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
Left by web development company on Aug 19, 2009 9:52 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
public string Strip(string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
} is the best
Left by timber on Aug 24, 2009 12:12 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
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;
}
Left by HasanG on Sep 13, 2009 3:37 PM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
Thanks Naren. Its very useful to me
Left by balaramesh on Nov 10, 2009 11:29 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
hi,
I had string like "good article <a href='http://'"
This is removed by Regex.Replace(@"[<*]","");
Left by balaramesh on Nov 10, 2009 11:32 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
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
%>
Left by kürtçe müzik dinle on May 26, 2010 12:15 AM

# re: Remove HTML Tags from HTML string
Requesting Gravatar...
fdsg s <br/>
Left by sfgss on Dec 27, 2011 8:56 PM

Your comment:
 (will show your gravatar)


Copyright © Narendra Tiwari | Powered by: GeeksWithBlogs.net