专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Asp教程 » 过滤html代码:html代码过滤方法及函数 »正文

过滤html代码:html代码过滤方法及函数

来源: 发布时间:星期四, 2008年9月25日 浏览:448次 评论:0
我们在开发过程中经常要用到的应该有HTML代码过滤这个函数吧,呵呵,很多人找来找去都不知道怎么实现,今天我来给大家贴一段代码轻松实现这个功能,好了来看代码吧
<scriptlanguage=\"javascript\">
publicstringcutStr(stringhtml)
{


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

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

//System.Text.RegularExpressions.Regexregex2=newSystem.Text.RegularExpressions.Regex(@\"<a[\\s\\S]+</a*>\",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

//System.Text.RegularExpressions.Regexregex3=newSystem.Text.RegularExpressions.Regex(@\"on[\\s\\S]*=\",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regexregex4=newSystem.Text.RegularExpressions.Regex(@\"<iframe[\\s\\S]+</iframe*>\",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regexregex5=newSystem.Text.RegularExpressions.Regex(@\"<frameset[\\s\\S]+</frameset*>\",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regexregex6=newSystem.Text.RegularExpressions.Regex(@\"<(.[^>]*)>\",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

html=regex1.Replace(html,\"\");//过滤<script>标记
html=regex4.Replace(html,\"\");//过滤iframe
html=regex5.Replace(html,\"\");//过滤frameset
html=regex6.Replace(html,\"\");//过滤其他标记
html=html.Replace(\"&nbsp;\",\"\");
html=html.Replace(\"\\n\",\"\");
html=html.Replace(\"\\r\",\"\");
html=html.Replace(\" \",\"\");
returnhtml;
[Page]
}
</script>

相关文章

读者评论

  • 1推荐一篇文章给你:(125.120.4.*) 发布于:星期三, 2008年12月3日10:09
    推荐一篇文章给你: 过滤html代码:html代码过滤方法及函数
  • 1 共1条 分1页

发表评论

  • 昵称:
  • 内容: