码迷,mamicode.com
首页 > Web开发 > 详细

清理网页中的HTML

时间:2015-09-10 07:19:03      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

 public string ClearHtml(string text)//过滤html,js,css代码
    {
        text = text.Trim();
        if (string.IsNullOrEmpty(text))
            return string.Empty;
        text = Regex.Replace(text, "<head[^>]*>(?:.|[\r\n])*?</head>", "");
        text = Regex.Replace(text, "<script[^>]*>(?:.|[\r\n])*?</script>", "");
        text = Regex.Replace(text, "<style[^>]*>(?:.|[\r\n])*?</style>", "");
        
        text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", ""); //<br> 
        text = Regex.Replace(text, "\\&[a-zA-Z]{1,10};", "");
        text = Regex.Replace(text, "<[^>]*>", "");

        text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", ""); // 
        text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //其它任何标记
        text = Regex.Replace(text, "[\\s]{2,}", " "); //两个或多个空格替换为一个

        text = text.Replace("‘", "‘‘");
        text = text.Replace("\r\n", "");
        text = text.Replace("  ", "");
        text = text.Replace("\t", "");
        return text.Trim();
    }

  

清理网页中的HTML

标签:

原文地址:http://www.cnblogs.com/babietongtianta/p/4796687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!