zoukankan
html css js c++ java
asp.net如何去掉HTML标记
/**/
///
<summary>
///
去除HTML标记
///
</summary>
///
<param name="NoHTML">
包括HTML的源码
</param>
///
<returns>
已经去除后的文字
</returns>
public
static
string
NoHTML(
string
Htmlstring)
{
//
删除脚本
Htmlstring
=
Regex.Replace(Htmlstring,
@"
<script[^>]*?>.*?</script>
"
,
""
,RegexOptions.IgnoreCase);
//
删除HTML
Htmlstring
=
Regex.Replace(Htmlstring,
@"
<(.[^>]*)>
"
,
""
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
([\r\n])[\s]+
"
,
""
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
-->
"
,
""
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
<!--.*
"
,
""
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(quot|#34);
"
,
"
\
""
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(amp|#38);
"
,
"
&
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(lt|#60);
"
,
"
<
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(gt|#62);
"
,
"
>
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(nbsp|#160);
"
,
"
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(iexcl|#161);
"
,
"
\xa1
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(cent|#162);
"
,
"
\xa2
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(pound|#163);
"
,
"
\xa3
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&(copy|#169);
"
,
"
\xa9
"
,RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
@"
&#(\d+);
"
,
""
,RegexOptions.IgnoreCase);
Htmlstring.Replace(
"
<
"
,
""
);
Htmlstring.Replace(
"
>
"
,
""
);
Htmlstring.Replace(
"
\r\n
"
,
""
);
Htmlstring
=
HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return
Htmlstring;
}
/**/
///
提取HTML代码中文字的C#函数
///
<summary>
///
去除HTML标记
///
</summary>
///
<param name="strHtml">
包括HTML的源码
</param>
///
<returns>
已经去除后的文字
</returns>
using
System;
using
System.Text.RegularExpressions;
public
class
StripHTMLTest
{
public
static
void
Main()
{
string
s
=
StripHTML(
"
<HTML><HEAD><TITLE>中国石龙信息平台</TITLE></HEAD><BODY>faddfs龙信息平台</BODY></HTML>
"
);
Console.WriteLine(s);
}
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;
}
}
写一个静态方法
移除HTML标签
#region
移除HTML标签
/**/
///
<summary>
///
移除HTML标签
///
</summary>
///
<param name="HTMLStr">
HTMLStr
</param>
public
static
string
ParseTags(
string
HTMLStr)
{
return
System.Text.RegularExpressions.Regex.Replace(HTMLStr,
"
<[^>]*>
"
,
""
);
}
#endregion
取出文本中的图片地址
#region
取出文本中的图片地址
/**/
///
<summary>
///
取出文本中的图片地址
///
</summary>
///
<param name="HTMLStr">
HTMLStr
</param>
public
static
string
GetImgUrl(
string
HTMLStr)
{
string
str
=
string
.Empty;
string
sPattern
=
@"
^<img\s+[^>]*>
"
;
Regex r
=
new
Regex(
@"
<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>
"
,
RegexOptions.Compiled);
Match m
=
r.Match(HTMLStr.ToLower());
if
(m.Success)
str
=
m.Result(
"
${url}
"
);
return
str;
}
#endregion
查看全文
相关阅读:
JavaScript ---Function
win7(x64)安装scrapy框架
[转]mysql性能优化-慢查询分析、优化索引和配置
[原创]win7环境下搭建eclipse+python+django开发环境
[原创]Python/Django使用富文本编辑器XHeditor上传本地图片
Ubuntu下mysql使用
[整理] mysql操作
[原创]Sql2008 使用TVP批量插入数据
一个js获取数组下标的函数
深入理解js的prototype以及prototype的一些应用
原文地址:https://www.cnblogs.com/goody9807/p/961195.html
最新文章
Ajax的初体验
apply方法和call方法的详解2
函数的属性和方法
css 之单行文本显示省略和多行文本省略
javascript之 原生document.querySelector和querySelectorAll方法
jquery 之节点操作
jquery尺寸和jQuery设置和获取内容方法
jquery 之事件 方法
jquery 之 $().each和$.each()
jquery-animate()动画
热门文章
springboot 日志框架
ubuntu下screen的使用
mysql 修改表结构
javax.xml.bind.UnmarshalException: 意外的元素 (uri:"", local:"xml")。所需元素为(none)
mysql 字段包含某个字符的函数
java 生成特定范围内的随机数
Mysql函数、语句
git 恢复误删的文件
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "FileSize"
java.lang.String 字符串操作
Copyright © 2011-2022 走看看