首页 > Web开发 > 详细

过滤字符串中的html标签

时间:2016-08-19 19:11:21      阅读:270      评论:0      收藏:0      [点我收藏+]

C#中,我们有时需要过滤掉字符串中的部分html标签,以下是一些简单的html标签过滤方法,使用的主要方式是正则表达式

public static string ClearHtml(string html)
        {
           if(string.IsNullOrEmpty(html))
            {
                return "";
            }
            //去除a标签
            html = Regex.Replace(html, @"<a\s*[^>]*>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"</a>", "", RegexOptions.IgnoreCase);
            //去除span标签
            html = Regex.Replace(html, @"<span\s*[^>]*>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"</span>", "", RegexOptions.IgnoreCase);
            //去除所有的样式
            html = Regex.Replace(html, @"\sstyle=""([^"";]+;?)+""", "", RegexOptions.IgnoreCase);

            //去除所有的html标签
            html = Regex.Replace(html, @"<[^>]*>|&nbsp;", "", RegexOptions.IgnoreCase);
            return html;
        }

过滤字符串中的html标签

原文:http://www.cnblogs.com/getpower/p/5788557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!