如何删除字符串中的所有HTML标签不知道哪些标记是吗? [英] How do I remove all HTML tags from a string without knowing which tags are in it?

查看:204
本文介绍了如何删除字符串中的所有HTML标签不知道哪些标记是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法来移除所有的HTML标签或任何HTML从一个字符串相关的?

Is there any easy way to remove all HTML tags or ANYTHING HTML related from a string?

例如:

string title = "<b> Hulk Hogan's Celebrity Championship Wrestling &nbsp;&nbsp;&nbsp;<font color=\"#228b22\">[Proj # 206010]</font></b>&nbsp;&nbsp;&nbsp; (Reality Series, &nbsp;)"

上面确实应该:

绿巨人霍根的名人冠军搏斗[PROJ#206010](真人秀节目)

"Hulk Hogan's Celebrity Championship Wrestling [Proj # 206010] (Reality Series)"

推荐答案

您可以使用一个简单的正则表达式是这样的:

You can use a simple regex like this:

public static string StripHTML(string input)
{
   return Regex.Replace(input, "<.*?>", String.Empty);
}

请注意,此解决方案都有自己的缺陷。查看删除在字符串 HTML标签的更多信息(尤其是@mehaase的评论)

Be aware that this solution has its own flaw. See Remove HTML tags in String for more information (especially the comments of @mehaase)

另一种解决方案是使用 HTML敏捷性包。结果
你可以在这里使用库找到一个例子:<一href=\"http://stackoverflow.com/questions/12787449/html-agility-pack-removing-unwanted-tags-without-removing-content\">HTML敏捷包 - 而不删除内容除去不需要的代码

Another solution would be to use the HTML Agility Pack.
You can find an example using the library here: HTML agility pack - removing unwanted tags without removing content?

这篇关于如何删除字符串中的所有HTML标签不知道哪些标记是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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