i use this method to convert html to plaint text but it have some bugs in this html tags <H1,2,3,..>
Method :
public string HtmlToPlainText(string htmlText)
{
//const string tagWhiteSpace = @"(>|$)(\W|\n|\r)+<";//matches one or more (white space or line breaks) between '>' and '<'
const string stripFormatting = @"<[^>]*(>|$)";//match any character between '<' and '>', even when end tag is missing
const string lineBreak = @"<(br|BR)\s{0,1}\/{0,1}>";//matches: <br>,<br/>,<br />,<BR>,<BR/>,<BR />
var lineBreakRegex = new Regex(lineBreak, RegexOptions.Multiline);
var stripFormattingRegex = new Regex(stripFormatting, RegexOptions.Multiline);
//var tagWhiteSpaceRegex = new Regex(tagWhiteSpace, RegexOptions.Multiline);
var text = htmlText;
//Decode html specific characters
text = System.Net.WebUtility.HtmlDecode(text);
//Remove tag whitespace / line breaks
//text = tagWhiteSpaceRegex.Replace(text, "><");
//Replace < br /> with line breaks
text = lineBreakRegex.Replace(text, Environment.NewLine);
//Strip formatting
text = stripFormattingRegex.Replace(text, string.Empty);
return text;
}
this is my html text :
<h3> This is a simple title </h3> </br> <p>Lorem ipsum <b> dolor sit </b> amet consectetur, <i>adipisicing elit.</i> </p>
This is my result :
This is a simple title Lorem ipsum dolor sit amet consectetur,
adipisicing elit.The result should be :
This is a simple title
Lorem ipsum dolor sit amet consectetur, adipisicing elit.
I think the error is from Strip formatting. How can i solve it?
` instead of ``? – Andrew Morton Dec 25 '21 at 13:07