提取HTML代码中文字的C#函数


/// <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;
}



相关阅读:
CSS如何控制网页字体?
安全威胁无孔不入:基于Linux系统的病毒
php SQLite学习笔记与常见问题分析
css 兼容性问题this.style.cursor=''''hand''''
何为XHTML??
javascript实例教程(8)
Atlas学习手记(27):JavaScript面向对象的扩展(一):命名空间Namespace
向进程发送信号:kill、killall和top
MySQL 有输入输出参数的存储过程实例
如何对PHP程序中的常见漏洞进行攻击
JavaScript面向对象------继承
说明PHP开发网站程序的优点
HTML5 语义化结构化规范化
HTML5 MiranaVideo播放器 (代码开源)
快速导航

Copyright © 2016 phpStudy | 皖ICP备18014864号-4