手机
当前位置:查字典教程网 >网页设计 >脚本HTML教程 >用C#过滤HTML代码的函数
用C#过滤HTML代码的函数
摘要:正好有时间所以用C#写了一段正则表达式,作用是删除Page里面Code中的HTML标签,这在做采集信息,消除其中的HTML很有用处。以下是引...

 正好有时间所以用C#写了一段正则表达式,作用是删除 Page 里面Code 中的 HTML标签,这在做采集信息,消除其中的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;
}

【用C#过滤HTML代码的函数】相关文章:

16进制颜色代码大全

XHTML的标准定义

如何禁用HTML页面的上下文菜单

HTML网页头部代码

用Java实现HTML文件代替数据库存储数据

数据库输出HTML格式报表的测试

看不到网页源代码方法汇总

hr 水平线样式示例代码

用HTML+CSS制作邮件网页的技巧总结

HTML5和以前HTML4的区别整理

精品推荐
分类导航