从html中删除所有文本 [英] Strip everything but text from html

查看:78
本文介绍了从html中删除所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的CMS允许用户使用html编辑器输入文本,所以当阅读文本到网页时,我可以这样写:

 &安培; #xD;&安培; #xA; < p为H.;&安培; #xD;&安培; #xA; < strong>文字文字。 more 
text< / strong>&#xD;&#xA; < a href =http:// blabla> blabla< / a>甚至更多文字...

我怎样才能去掉所有文本,包括,和。和类似的字符?

解析方案假设这是html(而不是xhtml) ,我会使用 HTML Agility Pack 进行分析,并访问 InnerText

  static void Main()
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(@& #xD;&#xA;< p>&#xD;&#xA; < strong> text text。more text< / strong>& #xD;&#xA;< a href =http:// blabla> blabla< / a>更多文字...);
string s = doc.DocumentNode.InnerText;
// s是:&#xD;&#xA;&#xD;&#xA; text text。more text&#xD ;&#xA;
}


Our CMS allows users to enter text using a html editor, so when reading text into the webpage I can text like this:

&#xD;&#xA;      <p>&#xD;&#xA;      <strong>text text. more 
text</strong>&#xD;&#xA;      <a href="http://blabla>blabla</a> even more text...

How can I strip everything but text including , and . and similar characters ?

解决方案

Assuming this is html (not xhtml), I would use the HTML Agility Pack to parse it, and access InnerText:

static void Main()
{
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(@"&#xD;&#xA;      <p>&#xD;&#xA;      <strong>text text. more text</strong>&#xD;&#xA;      <a href=""http://blabla>blabla</a> even more text...");
    string s = doc.DocumentNode.InnerText;
    // s is: &#xD;&#xA;      &#xD;&#xA;      text text. more text&#xD;&#xA;     
}

这篇关于从html中删除所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆