asp.net 去除数据中带有的html标签

 

1,在控制器中实现去除html标签的静态方法

//去除html标签
public static string ReplaceHtmlMark(object Contents)
{
string HtmlString = Convert.ToString(Contents);
string[] RegexString = {
@"style='.*?'",
@"class='.*?'",
@"<param.*?>(</param>)?",
@"<embed.*?>(</embed>)?",
@"<object.*?>(</object>)?",
@"<strong.*?>(</strong>)?",
@"<span.*?>(</span>)?",
@"<p.*?>(</p>)?",
@"<u.*?>(</u>)?",
@"<em.*?>(</em>)?",
@"<div.*?>(</div>)?",
@"<o:p.*?>(</o:p>)?",
@"<font.*?>(</font>)?",

};
foreach (String str in RegexString)
{
Regex regex = new Regex(str, RegexOptions.IgnoreCase);
HtmlString = regex.Replace(HtmlString, string.Empty);
}
string[] RegexString2 = {
@"</font>",
@"</o:p>",
@"</div>",
@"</p>",
@"</object>",
@"</strong>",
@"</span>",
@"</ins>",
@"&nbsp;",
};
foreach (String str2 in RegexString2)
{
Regex regex2 = new Regex(str2, RegexOptions.IgnoreCase);
HtmlString = regex2.Replace(HtmlString, string.Empty);
}
return HtmlString;
}

 

2,前台需要去除html标签的字段

  @(HomeController.ReplaceHtmlMark(dr["Content"]))

转载于:https://www.cnblogs.com/yushuo/p/3785531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值