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;
}
查看全文
相关阅读:
PHP程序员7小时学会Kotlin 第二小时
PHP程序员7小时学会Kotlin系列
PHP程序员7小时学会Kotlin系列
技术人员如何形成正确的价值观
景德镇特色的部门级别与权限
hhvm的正确安装姿势 http://dl.hhvm.com 镜像
有钱没钱
SB心结
优秀的技术Leader
BaaS模式的开发思路
原文地址:https://www.cnblogs.com/cnaspnet/p/1216790.html
最新文章
sparkR could not find function "textFile"
r指定位置插入一列数值
r指定位置插入一列
ggplot饼图
R笔记4:ggplot绘制商务图表--玫瑰图
在使用R做数据挖掘时,最常用的数据结构莫过于dataframe了,下面列出几种常见的dataframe的操作方法
R语言设置write.table()输出的文件格式
sparkR原理
python自定义方法处理日志文件
python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
热门文章
python中with的用法
python遍历目录os.walk(''d:\test2",topdown=False)
python模板字符串和格式化字符串
python字典的排序,按key排序和按value排序---sorted()
python练习:一行搞定-统计一句话中每个单词出现的个数
python深拷贝和浅拷贝的区别
python迭代器以及生成器
python练习题-打印斐波拉契数列前n项
Netbeans 8.2将支持PHP 7
项目中初试PHP单元测试
Copyright © 2011-2022 走看看