首页 > 编程 > HTML > 正文

用C-#过滤HTML代码的函数

2020-03-24 15:56:03
字体:
来源:转载
供稿:网友

正好有时间所以用C#写了一段html' target='_blank'>正则表达式,作用是删除 Page 里面Code 中的 HTML标签

文艺网名[www.la240.com/html2017/1/33/],这在做采集信息,消除其中的HTML很有用处。

以下是引用片段:

publicstringcheckStr(stringhtml)

{

System.Text.RegularExpressions.Regexregex1=newSystem.Text.RegularExpressions.Regex(@"<script[sS]+</script*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex2=newSystem.Text.RegularExpressions.Regex(@"href*=*[sS]*script*:",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex3=newSystem.Text.RegularExpressions.Regex(@"no[sS]*=",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex4=newSystem.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex5=newSystem.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex6=newSystem.Text.RegularExpressions.Regex(@"<img[^>]+>",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 

System.Text.RegularExpressions.Regexregex7=newSystem.Text.RegularExpressions.Regex(@"</p>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex8=newSystem.Text.RegularExpressions.Regex(@"<p>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.Regexregex9=newSystem.Text.RegularExpressions.Regex(@"<[^>]*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

html=regex1.Replace(html,"");//过滤<script></script>标记

html=regex2.Replace(html,"");//过滤href=javascript:(<A>)属性

html=regex3.Replace(html,"_disibledevent=");//过滤其它控件的on...事件

html=regex4.Replace(html,"");//过滤iframe

html=regex5.Replace(html,"");//过滤frameset

html=regex6.Replace(html,"");//过滤frameset

html=regex7.Replace(html,"");//过滤frameset

html=regex8.Replace(html,"");//过滤frameset

html=regex9.Replace(html,"");

html=html.Replace("","");

html=html.Replace("</strong>","");

html=html.Replace("<strong>","");

returnhtml;

}

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表