清除Html标记的一个封装函数
2012-08-27 13:14
531 查看
后台.cs文件代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
///AboutClearHtml 的摘要说明
/// </summary>
public class AboutClearHtml
{
public AboutClearHtml()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region 清除HTML函数
/// <summary>
/// 清除HTML标记函数
/// </summary>
/// <param name="Htmlstring">带Html标记的字符串</param>
/// <returns>不带Html的字符串</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;
}
#endregion
}
网页中引用:
<a style="cursor: pointer" href="WebPages/ShowData.aspx?SID=<%#Eval("SID") %>">
<%#AboutClearHtml.NoHTML(Eval("DataContent").ToString()).ToString().Length >80?AboutClearHtml.NoHTML(Eval("DataContent").ToString()).ToString().Substring (0,80)+"......": AboutClearHtml.NoHTML(Eval("DataContent").ToString())%>
</a>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
///AboutClearHtml 的摘要说明
/// </summary>
public class AboutClearHtml
{
public AboutClearHtml()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region 清除HTML函数
/// <summary>
/// 清除HTML标记函数
/// </summary>
/// <param name="Htmlstring">带Html标记的字符串</param>
/// <returns>不带Html的字符串</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;
}
#endregion
}
网页中引用:
<a style="cursor: pointer" href="WebPages/ShowData.aspx?SID=<%#Eval("SID") %>">
<%#AboutClearHtml.NoHTML(Eval("DataContent").ToString()).ToString().Length >80?AboutClearHtml.NoHTML(Eval("DataContent").ToString()).ToString().Substring (0,80)+"......": AboutClearHtml.NoHTML(Eval("DataContent").ToString())%>
</a>
相关文章推荐
- SQL Server 2008 clearhtml函数清除字段中的HTML标记
- PHP封装的一个支持HTML、JS、PHP重定向的多功能跳转函数
- 一个封装HTTP请求的函数(C++)
- CodeProject - 在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- ASP.NET 去除HTML标记 函数
- bmp格式图像的读写函数(对一个开源代码的封装)
- 收藏——在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- ScriptHtml 函数之过滤html标记的asp代码
- 一个封装好的XMLHttp对象的创建函数
- 用 JavaScript 写 ASP,似乎很爽啊,自己封装了一个 操作数据库 分页 以及 文件操作的函数
- js封装getByClass (JS获取class的方法封装为一个函数)的API
- 封装getByClass(JS获取class的方法封装为一个函数)
- HTML学习笔记之中的一个(input文件选择框的封装)
- 封装一个函数afterDate(date,n),得到日期date的n天后的日期
- PHP压缩html网页代码(清除空格,换行符,制表符,注释标记)
- 【叶子函数分享一】去除字符串中的html标记及标记中的内容
- bmp格式图像的读写函数(对一个开源代码的封装)
- 封装了一个发邮件的dll,支持附件,HTML
- 封装PHP mail函数发送HTML邮件并使用中文发件人且自定义发件人邮箱地址
- 用php自带函数去除html标记