惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
B
Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
U
Unit 42
雷峰网
雷峰网
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
美团技术团队
Y
Y Combinator Blog
腾讯CDC
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements

博客园 - 偶然微笑

asp.net 登陆 asp.net获取URL和IP地址 ASP.NET获取IP与MAC地址的方法 ASP.NET获取IP的6种方法 - 偶然微笑 - 博客园 MS SQL Server 2005 通用分页存储过程 又快又简单的sql2005分页存储过程 SQL SERVER 2005分页存储过程 C#区别和认识四个判等函数 C#中数字日期转中文日期 C#算法(一)选择排序 创建基于ASP.NET的SMTP邮件服务 url传递中文的解决方案总结 C#如何取硬件标志 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie Asp.Net输出数据到EXCEL中 把文字变成图片的小程序 ASP.NET URL Rewrite. URL重写 asp.net采集函数(采集、分析、替换、入库) .net 无限级分类
提取HTML代码中文字的C#函数
偶然微笑 · 2008-09-20 · via 博客园 - 偶然微笑

/// <summary>
  
/// 去除HTML标记
  
/// </summary>
  
/// <param name="strHtml">包括HTML的源码 </param>
  
/// <returns>已经去除后的文字</returns>

  public static string StripHTML(string strHtml)
  
{
   
string [] aryReg ={
          
@"<script[^>]*?>.*?</script>",

          
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
          
@"([\r\n])[\s]+",
          
@"&(quot|#34);",
          
@"&(amp|#38);",
          
@"&(lt|#60);",
          
@"&(gt|#62);"
          
@"&(nbsp|#160);"
          
@"&(iexcl|#161);",
          
@"&(cent|#162);",
          
@"&(pound|#163);",
          
@"&(copy|#169);",
          
@"&#(\d+);",
          
@"-->",
          
@"<!--.*\n"
         
         }
;

   
string [] aryRep = {
           
"",
           
"",
           
"",
           
"\"",
           "&",
           
"<",
           
">",
           
" ",
           
"\xa1",//chr(161),
           "\xa2",//chr(162),
           "\xa3",//chr(163),
           "\xa9",//chr(169),
           "",
           
"\r\n",
           
""
          }
;

   
string newReg =aryReg[0];
   
string strOutput=strHtml;
   
for(int i = 0;i<aryReg.Length;i++)
   
{
    Regex regex 
= new Regex(aryReg[i],RegexOptions.IgnoreCase );
    strOutput 
= regex.Replace(strOutput,aryRep[i]);
   }


   strOutput.Replace(
"<","");
   strOutput.Replace(
">","");
   strOutput.Replace(
"\r\n","");


   
return strOutput;
  }