如何去除所有标签并获取纯文本?

4

我需要将用户输入的文本以 HTML 和 CSS 格式存储到我的数据库中。

情况是这样的:

RadEditor,用户从 MSWord 中复制文本到此编辑器,然后我使用该格式将此文本存储在数据库中。然后,在报告或某些标签中检索数据时,会出现一些标记包裹文本!

我使用正则表达式删除所有格式,但有时成功,有时不成功。

private static Regex oClearHtmlScript = new Regex(@"<(.|\n)*?>", RegexOptions.Compiled);

        public static string RemoveAllHTMLTags(string sHtml)
        {

            sHtml = sHtml.Replace("&nbsp;", string.Empty);
            sHtml = sHtml.Replace("&gt;", ">");
            sHtml = sHtml.Replace("&lt;", "<");
            sHtml = sHtml.Replace("&amp;", "&");
            if (string.IsNullOrEmpty(sHtml))
                return string.Empty;

            return oClearHtmlScript.Replace(sHtml, string.Empty);
        }

我想知道如何使用HTMLAgility或其他可靠的方法删除所有格式,以确保文本是纯净的?

注意:数据库中此字段的数据类型为Lvarchar


在我的答案中添加了另一个建议,因为第一个似乎没有起作用。 - Luke Baughan
4个回答

3

HtmlAgility pack使得处理HTML变得更加容易。

HtmlDocument mainDoc = new HtmlDocument();
string htmlString = "<html><body><h1>Test</h1> more text</body></html>"
mainDoc.LoadHtml(htmlString);
string cleanText = mainDoc.DocumentNode.InnerText;

3
这段代码可以从字符串中去除所有的html标签。
sHtml = Regex.Replace(sHtml, "<.*?>", "");

它需要是有效的HTML标签 - 以<开头,以>结尾。例如,<span style="font-size: 16pt; font-family: Simplified">test</span> 的结果为 test - Win
<p style="margin-right:3.5in;text-align:left;text-indent:0.5in;line-height:150%;direction:rtl;unicode-bidi:embed;" dir="RTL"><strong><span style="line-height:150%;">رقم</span></strong></p><p style="margin-right:3.5in;text-align:left;text-indent:0.5in;line-height:150%;direction:rtl;unicode-bidi:embed;" dir="RTL"><strong><span style="line-height:150%;">  </span></strong></p><p style="text-align:right;line-height:150%;direction:rtl;unicode-bidi:embed;" dir="RTL"><strong><span style="line-height:150%;"> </span></strong></p> - Anyname Donotcare
</strong></p><p style="text-align: right; direction: rtl; unicode-bidi: embed;" dir="RTL"><span style="font-size: 16pt; font-family: Simplified Arabic,serif;">&nbsp;</span></p><p style="text-align: right; direction: rtl; unicode-bidi: embed;" dir="RTL"><strong><span style="font-size: 16pt; font-family: Simplified Arabic,serif;">أحمد</span></strong></p><p style="margin-right: 2.5in; text-align: right; text-indent: 0.5in; direction: rtl; unicode-bidi: embed;" dir="RTL"><strong><span style="font-size: 16pt; font-family: Simplified Arabic,serif;">&nbsp; </span></strong><strong> </strong></p> - Anyname Donotcare
<p style="text-align: right; direction: rtl; unicode-bidi: embed;" dir="RTL"><span style="font-size: 16pt; font-family: Simplified Arabic,serif;">&nbsp;</span></p><p style="text-align: right; direction: rtl; unicode-bidi: embed;" dir="RTL"><strong><span style="font-size: 16pt; font-family: Simplified Arabic,serif;">تحية </span></strong></p><p style="text-align: right; direction: rtl; unicode-bidi: embed;" dir="RTL"><span style="font-size: 16pt; font-family: Simplified Arabic,serif;">نكتب </span> - Anyname Donotcare
这个 bug 怎么修啊 :( - Anyname Donotcare
显示剩余3条评论

2

这篇帖子推荐以下方法(并且似乎已经被接受)。

Regex.Replace(myHTMLString, @"<p>|</p>|<br>|<br />", "\r\n", );
Regex.Replace(myHTMLString, @"<.+?>", string.Empty);

如果您仍然有困难,请尝试实例化RadEditor并使用.Text属性。我以前没有使用过RadEditor,但是我找到了一些信息 - 您可以尝试类似于以下内容:

RadEditor editor = new RadEditor();
editor.Content = myHTMLString;
string plainText = editor.Text;

这可能是一项非常昂贵的操作,但我很想知道它是否起作用!

仍然存在同样的问题,例如:<span style="font-size: 16pt; font-family: Simplified - Anyname Donotcare

1

请查看这里获取使用敏捷包进行操作的答案。您可能需要稍微更改代码,以便不会剥离少于两个字符的单词。此外,换行符也将被删除,因此您将得到一行长文本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接