第一次自己做网站,需要过滤掉用户输入信息中的HTML代码、WORD文本的样式,还有一些SQL关键字,怎么做呢?越详细越好!

解决方案 »

  1.   

    /**/ 
    ///   <summary>
    ///   去除HTML标记
    ///   </summary>
    ///   <param   name="NoHTML">包括HTML的源码   </param>
    ///   <returns>已经去除后的文字</returns>
    public static string NoHTML(string Htmlstring)
    {
    //删除脚本
    Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
        RegexOptions.IgnoreCase);
    //删除HTML
    Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
        RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
        RegexOptions.IgnoreCase); 
    Htmlstring.Replace("<", "");
    Htmlstring.Replace(">", "");
    Htmlstring.Replace("\r\n", "");
    Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();return Htmlstring;
    }
     
      

  2.   

    这个也只能去除HTML中的一部分代码,其他的HTML代码、SQL和WORD格式怎么去掉呢?
      

  3.   

    sql 注入过滤
    public static string InputText(string text, int maxLength) 
        { 
            text = Regex.Replace(text, "[\\s]{2,}", " "); 
            text = Regex.Replace(text, "( <[b|B][r|R]/*>)+|( <[p|P](.|\\n)*?>)", "\n"); 
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); 
            text = Regex.Replace(text, " <(.|\\n)*?>", string.Empty); 
            text = text.Replace("'", "''"); 
            return text; 
        }
    http://topic.csdn.net/u/20090708/09/b78444ee-9081-4ff7-8aa5-ba6f9b1d9fdc.html
      

  4.   

    html=Regex.Replace(html,@"<[^>]+>",   " ",   RegexOptions.IgnoreCase); 
      

  5.   

    我现在非常想知道如何去掉WORD样式!!!!