ASP.NET去除HTML标签并截取指定长度字符串
2017-05-31 10:56
801 查看
当我们使用编辑器存储内容时,保存到数据库的内容可含有html标签,如果要将其查询出来展示在页面上时有时会用到字符串截取,这时我们应该先对其进行html解码,再去html标签,最后再截取指定长度字符串。
要显示变量SNote的前20个字,调用方法如下:
/// <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)
相关文章推荐
- asp.net 安全的截取指定长度的html或者ubb字符串
- asp.net 截取带有html标签的字符串(先过滤html,再截取)的方法
- asp.net在前台将指定字符串按指定长度进行截取并用其他字符串替换方法
- asp.net 将指定字符串按指定长度进行截取并用其他字符串替换方法
- asp.net去除字符串中html标签
- (ASP.NET )去除字符串中的HTML标签
- asp.net指定长度截取字段省略(中英文混合字符串)
- 织梦截取字符长度和去除HTML标签的函数
- php读取富文本编辑器编辑后的文章内容,并去除所有html标签、空格以及空白,截取字符串(包括中文)
- asp.net获取html内容中的指定标签下的某个属性值
- asp.net 去除数据中带有的html标签
- C#去除指定字符串中的HTML标签相关代码函数
- 帝国 标签模板 使用程序代码 去除html标记 并 截取字符串
- asp.net截取指定字符后面的字符串
- [Asp.Net]按字符串实际长度截取定长字符窜
- ASP.Net 去除字符串中的html标记
- 猫猫学iOS之去除服务器返回数据中的html标签,去除指定字符串,替换字符串
- asp.net 截取html标签取对应值
- 指定HTML标签属性 |Specifying HTML Attributes| 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼