过滤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(\" \",\"\"); html=html.Replace(\"\\n\",\"\"); html=html.Replace(\"\\r\",\"\"); html=html.Replace(\" \",\"\"); returnhtml; [Page] } </script> 1
相关文章
读者评论
发表评论 |