zoukankan
html css js c++ java
过滤HTML字符显示摘要
public
static
string
GetSummary(
string
html,
int
summaryLength)
{
string
text
=
GetHtmlText(html);
text
=
DelHTML(text);
text
=
Strtitle(text);
if
(summaryLength
>=
text.Length)
return
text;
return
text.Substring(
0
, summaryLength)
+
"
"
;
}
public
static
string
GetHtmlText(
string
html)
{
return
System.Text.RegularExpressions.Regex.Replace(html,
@"
<[^>]*>
"
,
""
);
}
public
static
string
DelHTML(
string
Htmlstring)
//
将HTML去除
{
#region
//
删除脚本
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
<script[^>]*?>.*?</script>
"
,
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//
删除HTML
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
<(.[^>]*)>
"
,
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
([\r\n])[\s]+
"
,
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
-->
"
,
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
<!--.*
"
,
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<A>.*</A>","");
//
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<[a-zA-Z]*=\.[a-zA-Z]*\?[a-zA-Z]+=\d&\w=%[a-zA-Z]*|[A-Z0-9]","");
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(quot|#34);
"
,
"
\
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(amp|#38);
"
,
"
&
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(lt|#60);
"
,
"
<
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(gt|#62);
"
,
"
>
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(nbsp|#160);
"
,
"
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(iexcl|#161);
"
,
"
\xa1
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(cent|#162);
"
,
"
\xa2
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(pound|#163);
"
,
"
\xa3
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&(copy|#169);
"
,
"
\xa9
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring
=
System.Text.RegularExpressions.Regex.Replace(Htmlstring,
@"
&#(\d+);
"
,
""
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring.Replace(
"
<
"
,
""
);
Htmlstring.Replace(
"
>
"
,
""
);
Htmlstring.Replace(
"
\r\n
"
,
""
);
//
Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
#endregion
return
Htmlstring.Trim();
}
public
static
string
DelWords(
string
strtitle)
{
string
strContent
=
strtitle;
strtitle
=
Regex.Replace(strContent,
"
(<H1.*?>|</H1>|</br>|<SPAN.*?>|</SPAN>|<FONT.*?>||<o:p></o:p>)
"
,
""
);
strContent
=
strtitle;
strContent
=
Regex.Replace(strContent,
"
(<IMG.*?>)
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
(<P.*?>)
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
(<P>)
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
( )
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
(”)
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
(</P>)
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
(</BR>)
"
,
""
);
strContent
=
Regex.Replace(strContent,
"
(<BR>)
"
,
""
);
strContent
=
strContent.Replace(
"
<?xml:namespace prefix = o ns = 'urn:schemas-microsoft-com:office:office'/>
"
,
""
);
return
strtitle;
}
查看全文
相关阅读:
Web Services开发(Java篇)
前端UI框架小汇总
WebService 在C#.NET中的使用
求n*m网格内矩形的数目
Silverlight学习笔记(三):控件布局管理
Silverlight学习笔记(二):创建Hello World 项目(备注)
ExcelHelp 学习笔记二: C# 往Excel中写入数据
DataBase 学习笔记一:c#连接SQL数据库
ExcelHelp 学习笔记一: C#读取Excel中数据
Silverlight学习笔记(二):创建Hello World 简单基础应用
原文地址:https://www.cnblogs.com/cnaspnet/p/1216790.html
最新文章
微信分享不显示正确图标问题
关于SQL数据库建立表
iframe解决ajax主域和子域之间的跨域问题
循环ajax请求问题
解决input标签placeholder属性浏览器兼容性问题的一种方法
【译】理解node.js事件轮询
nodejs cannot find module 'mysql' 问题分析
div居中布局
输入框规则验证
响应式瀑布流插件Grid-A-Licious
热门文章
jquery中的load方法加载页面无法缓存问题
第一个Struts2入门经典实例
Structs2及其执行原理
MVC的基本概念
Tomcat环境的配置与部署Web应用
java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils 错误解决方法
oracle入门笔记
Java编写99乘法表
SQL 求解每月周末天数
SQL Server如何链接到 Oracle并查询其中的数据?并实现做接口
Copyright © 2011-2022 走看看