ASP.NET去除HTML标签并截取指定长度字符串
来源:互联网 发布:科密考勤机数据库下载 编辑:程序博客网 时间:2024/05/23 19:39
当我们使用编辑器存储内容时,保存到数据库的内容可含有html标签,如果要将其查询出来展示在页面上时有时会用到字符串截取,这时我们应该先对其进行html解码,再去html标签,最后再截取指定长度字符串。
/// <summary>/// 去除html标签/// </summary>/// <param name="htmlStr"></param>/// <returns></returns>public static string NoHTML(string htmlStr){ if (htmlStr == null) { return ""; } else { //删除脚本 htmlStr = Regex.Replace(htmlStr, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML htmlStr = Regex.Replace(htmlStr, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"([rn])[s]+", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"-->", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"<!--.*", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(quot|#34);", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(iexcl|#161);", "xa1", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(cent|#162);", "xa2", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(pound|#163);", "xa3", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&(copy|#169);", "xa9", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, @"&#(d+);", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, "xp_cmdshell", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, " ", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, "/r", "", RegexOptions.IgnoreCase); htmlStr = Regex.Replace(htmlStr, "/n", "", RegexOptions.IgnoreCase); //特殊的字符 htmlStr = htmlStr.Replace("<", ""); htmlStr = htmlStr.Replace(">", ""); htmlStr = htmlStr.Replace("*", ""); htmlStr = htmlStr.Replace("-", ""); htmlStr = htmlStr.Replace("?", ""); htmlStr = htmlStr.Replace(",", ""); htmlStr = htmlStr.Replace("/", ""); htmlStr = htmlStr.Replace(";", ""); htmlStr = htmlStr.Replace("*/", ""); htmlStr = htmlStr.Replace("rn", ""); htmlStr = HttpContext.Current.Server.HtmlEncode(htmlStr).Trim(); return htmlStr; }}/// <summary>/// 截取指定长度中英文字符串方法/// 该方法是按照每个汉字两个字节计算,∴如要截取20个字符,需要将length设置为40/// </summary>/// <param name="stringToSub"></param>/// <param name="length"></param>/// <returns></returns>public static string GetFirstString(string stringToSub, int length){ Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled); char[] stringChar = stringToSub.ToCharArray(); StringBuilder sb = new StringBuilder(); int nLength = 0; bool isCut = false; for (int i = 0; i < stringChar.Length; i++) { if (regex.IsMatch((stringChar[i]).ToString())) { sb.Append(stringChar[i]); nLength += 2; } else { sb.Append(stringChar[i]); nLength = nLength + 1; } if (nLength > length) { isCut = true; break; } } if (isCut) return sb.ToString() + ".."; else return sb.ToString();}
要显示变量SNote的前20个字,调用方法如下:
GetFirstString(NoHTML(Server.HtmlDecode(SNote)), 40)
阅读全文
0 0
- ASP.NET去除HTML标签并截取指定长度字符串
- ASP.NET 去除字符串html样式标签
- Sql版本除去html并截取指定长度的字符串
- asp.net 截取带有html标签的字符串(先过滤html,再截取)的方法
- 去除指定字符串中的HTML标签
- C#去除指定字符串中的HTML标签
- 按字节长度截取字符串(支持截取带HTML标签的字符串,并自动补全)工具类
- 截取指定长度字符串
- asp.net指定长度截取字段省略(中英文混合字符串)
- asp.net在前台将指定字符串按指定长度进行截取并用其他字符串替换方法
- smarty 去除html标签,并截取后加\"...\"
- ASP.NET 去除网页的html标签
- ASP.NET截取指定长度汉字超出部分以"..."代替
- ASP.Net 去除字符串中的html标记
- freemarker截取指定长度字符串
- FreeMaker 截取指定长度字符串
- freemarker截取指定长度字符串
- freemarker截取指定长度字符串
- C++第三课------默认构造函数
- 策略模式StrategyPattern
- 基于Bootstrap简洁的后台UI框架
- Sqoop使用手册
- win10或win8下安装msi格式安装包报错(error code is 2502/2503)
- ASP.NET去除HTML标签并截取指定长度字符串
- php7 wordpress 报错修改
- 页面浮窗简单实现
- 安卓智能地图开发与实施十三:空间查询与展示
- activemq入门
- JAVA读取文件夹大小的几种方式
- Solr--Solr的安装和配置
- RequestUtils
- 微信集成你可能遇到的问题