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;
}
查看全文
相关阅读:
js 获取和设置css3 属性值的实现方法
API的自动化测试
删除html标签或标签属性以及样式
JS+CSS实现数字滚动
video元素和audio元素相关事件
SDT v0.0.1 上线
safari浏览器fixed后,被软键盘遮盖的问题—【未解决】
js中DOM事件探究
使用Web存储API存取本地数据
剑指offer(Java版)第七题:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead, 分别完成在队列尾部插入结点和在队列头部删除结点的功能。
原文地址:https://www.cnblogs.com/cnaspnet/p/1216790.html
最新文章
【2019-10-08】保证自己的航向是正确的
【2019-10-07】害怕之后不要发怒,那种胆小的糟糕
【2019-10-06】旅游是花钱买自己家里的好
【2019-10-05】把目光投向如何定义问题
【2019-10-04】美丽的报复
【2019-10-03】唯有良善可谓普世
【2019-10-02】每个人都是探险家
【2019-10-01】我想与我思
【一句日历】2019年10月
unique() sstream
热门文章
Summer training #6
基础简单DP
Summer training #4
Summer training #5
Codeforces Round #423
博弈论BOSS
Game theory
NIM 1
Summer training #3
CSS 常用的定位和布局方法汇总(已添加源码地址)
Copyright © 2011-2022 走看看