.net过滤特定代码
#region///过滤html,js,css代码
///<summary>
///过滤html,js,css代码
///</summary>
///<paramname="html">参数传入</param>
///<returns></returns>
publicstaticstringCheckStr(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.Regexregex3=newSystem.Text.RegularExpressions.Regex(@"no[\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(@"\<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,"");//过滤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;
}
#endregion
#region///过滤p/p代码
///<summary>
///过滤p/p代码
///</summary>
///<paramname="html">参数传入</param>
///<returns></returns>
publicstaticstringInputStr(stringhtml)
{
html=html.Replace(@"\<img[^\>]+\>","");
html=html.Replace(@"<p>","");
html=html.Replace(@"</p>","");
returnhtml;
}
#endregion
///<summary>
///截取字符串
///</summary>
///<paramname="str"></param>
///<paramname="length"></param>
///<returns></returns>
protectedstringGetLength(stringstr,intlength)
{
stringstrR=str;
if(str.Length>length)
{
strR=str.Substring(0,length)+"...";
}
returnstrR;
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。