.Net 2005过滤危险字符的方法

做网站类似留言的时候可以用到的过滤危险字符的方法

路漫漫其修远兮..........


     代码如下: 

  public string NoHTML(string Htmlstring)
        {
            string strOutput = Htmlstring;

            strOutput = Regex.Replace(strOutput, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //   strOutput = Regex.Replace(strOutput,@"<(///s*)?!?((/w+:)?/w+)(/w+(/s*=?/s*(([""'])(//[""'tbnr]|[^/7])*?/7|/w+)|.{0})|/s)*?(///s*)?>","",RegexOptions.IgnoreCase);//在个别情况下会引起IIS吃爆CPU的现象
            strOutput = Regex.Replace(strOutput, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"-->", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"<!--.*", "", RegexOptions.IgnoreCase);

            strOutput = Regex.Replace(strOutput, @"&(quot|#34);", "/"", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&#(/d+);", "", RegexOptions.IgnoreCase);


            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("/r/n", "");
            strOutput = HttpContext.Current.Server.HtmlEncode(strOutput).Trim();

            return strOutput;
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值