我想使用C#将html转换为纯文本到目前为止我已实现此代码: - >
public static string HTMLToText(string HTMLCode)
{
if (HTMLCode == null)
return null;
// Remove new lines since they are not visible in HTML
HTMLCode = HTMLCode.Replace("\n", " ");
// Remove tab spaces
HTMLCode = HTMLCode.Replace("\t", " ");
// Remove multiple white spaces from HTML
HTMLCode = System.Text.RegularExpressions.Regex.Replace(HTMLCode, "\\s+", " ");
// Remove HEAD tag
HTMLCode = System.Text.RegularExpressions.Regex.Replace(HTMLCode, "<head.*?</head>", ""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
// Remove any JavaScript
HTMLCode = System.Text.RegularExpressions.Regex.Replace(HTMLCode, "<script.*?</script>", ""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
// Replace special characters like &, <, >, " etc.
StringBuilder sbHTML = new StringBuilder(HTMLCode);
// Note: There are many more special characters, these are just
// most common. You can add new characters in this arrays if needed
string[] OldWords = { " ", "&", """, "<", ">", "®", "©", "•", "™" };
string[] NewWords = { " ", "&", "\"", "<", ">", "®", "©", "•", "™" };
for (int i = 0; i < OldWords.Length; i++)
{
sbHTML.Replace(OldWords[i], NewWords[i]);
}
// Check if there are line breaks (<br>) or paragraph (<p>)
sbHTML.Replace("<br>", "\n");
sbHTML.Replace("<br ", "\n ");
sbHTML.Replace("<p> ", "\n ");
sbHTML.Replace("<p ", "\n ");
sbHTML.Replace("<span ", "\n ");
sbHTML.Replace("style", " ");
sbHTML.Replace("FONT", " ");
sbHTML.Replace("FONT-SIZE", " ");
sbHTML.Replace("</span> ", "\n");
// Finally, remove all HTML tags and return plain text
//Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
return System.Text.RegularExpressions.Regex.Replace(sbHTML.ToString(), @"</?[a-z][a-z0-9][^<>]>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
但它不会删除所有标签以及<p>.....<p>...</p>..</p>
之类的递归标签
所以外部<p>
被移除但内部标记仍然是他们的......
他们是否可以获得纯文本?