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

.NET常用方法收藏

时间:2015-07-07 09:17:46      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

1、过滤文本中的HTML标签

技术分享
 1 /// <summary>  
 2     /// 清除文本中Html的标签  
 3     /// </summary>  
 4     /// <param name="Content"></param>  
 5     /// <returns></returns>  
 6     public static string ClearHtml(string Content)
 7     {
 8         Content = ReplaceHtml("&#[^>]*;", "", Content);
 9         Content = ReplaceHtml("</?marquee[^>]*>", "", Content);
10         Content = ReplaceHtml("</?object[^>]*>", "", Content);
11         Content = ReplaceHtml("</?param[^>]*>", "", Content);
12         Content = ReplaceHtml("</?embed[^>]*>", "", Content);
13         Content = ReplaceHtml("</?table[^>]*>", "", Content);
14         Content = ReplaceHtml(" ", "", Content);
15         Content = ReplaceHtml("</?tr[^>]*>", "", Content);
16         Content = ReplaceHtml("</?th[^>]*>", "", Content);
17         Content = ReplaceHtml("</?p[^>]*>", "", Content);
18         Content = ReplaceHtml("</?a[^>]*>", "", Content);
19         Content = ReplaceHtml("</?img[^>]*>", "", Content);
20         Content = ReplaceHtml("</?tbody[^>]*>", "", Content);
21         Content = ReplaceHtml("</?li[^>]*>", "", Content);
22         Content = ReplaceHtml("</?span[^>]*>", "", Content);
23         Content = ReplaceHtml("</?div[^>]*>", "", Content);
24         Content = ReplaceHtml("</?th[^>]*>", "", Content);
25         Content = ReplaceHtml("</?td[^>]*>", "", Content);
26         Content = ReplaceHtml("</?script[^>]*>", "", Content);
27         Content = ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content);
28         Content = ReplaceHtml("on(mouse|exit|error|click|key)", "", Content);
29         Content = ReplaceHtml("<\\?xml[^>]*>", "", Content);
30         Content = ReplaceHtml("<\\/?[a-z]+:[^>]*>", "", Content);
31         Content = ReplaceHtml("</?font[^>]*>", "", Content);
32         Content = ReplaceHtml("</?b[^>]*>", "", Content);
33         Content = ReplaceHtml("</?u[^>]*>", "", Content);
34         Content = ReplaceHtml("</?i[^>]*>", "", Content);
35         Content = ReplaceHtml("</?strong[^>]*>", "", Content);
36 
37         Content = Regex.Replace(Content.Trim(), "\\s+", " ");
38         string clearHtml = Content;
39         return clearHtml;
40     }
41 
42     /// <summary>  
43     /// 清除文本中的Html标签  
44     /// </summary>  
45     /// <param name="patrn">要替换的标签正则表达式</param>  
46     /// <param name="strRep">替换为的内容</param>  
47     /// <param name="content">要替换的内容</param>  
48     /// <returns></returns>  
49     private static string ReplaceHtml(string patrn, string strRep, string content)
50     {
51         if (string.IsNullOrEmpty(content))
52         {
53             content = "";
54         }
55         Regex rgEx = new Regex(patrn, RegexOptions.IgnoreCase);
56         string strTxt = rgEx.Replace(content, strRep);
57         return strTxt;
58     }
HTML标记过滤

 

.NET常用方法收藏

标签:

原文地址:http://www.cnblogs.com/loyung/p/4625974.html

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