公告
财富商城
积分规则
提问
发文
2019-01-03 02:27发布
Luminary・发光体
asp.net razor 如何去掉html标签,只显示文字并限制最多字数? 如下图。
这种东西一般就是,要么不考虑性能什么的,用正则,简便;要么就找专门的HTML解析程序。。。
//去掉html标签函数 @functions{ public static string RemoveHtml(String html) { string text = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", ""); text = System.Text.RegularExpressions.Regex.Replace(text, "&[^;]+;", ""); return text; } } //其他页面调用函数(helpers为函数的页面名字) @helpers.RemoveHtml(item.Content)}
写个正则过滤掉html标签 后台过滤:
//删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除<style></style>样式 Htmlstring = Regex.Replace(Htmlstring, @"<style[^>]*?>[\s\S]*?</style>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(?!img|p|/p|br).*?>", "", RegexOptions.IgnoreCase); //Htmlstring = Regex.Replace(Htmlstring, "/(?<=\" )style=\".*?\"/", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "style=\".+?\"", "", 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;
如图,什么意思??
考虑用js正则去掉html标签即可
去网上找个正则去掉文本中的html标签,再截取长度
参考http://blog.csdn.net/gulijiang2008/article/details/7190281中的
//// <summary> /// 去除HTML标记 /// </summary> /// <param name="NoHTML">包括HTML的源码 </param> /// <returns>已经去除后的文字</returns> public static string NoHTML(string Htmlstring)
最多设置5个标签!
这种东西一般就是,要么不考虑性能什么的,用正则,简便;要么就找专门的HTML解析程序。。。
写个正则过滤掉html标签 后台过滤:
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除<style></style>样式
Htmlstring = Regex.Replace(Htmlstring, @"<style[^>]*?>[\s\S]*?</style>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(?!img|p|/p|br).*?>", "", RegexOptions.IgnoreCase);
//Htmlstring = Regex.Replace(Htmlstring, "/(?<=\" )style=\".*?\"/", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "style=\".+?\"", "", 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;
如图,什么意思??
考虑用js正则去掉html标签即可
去网上找个正则去掉文本中的html标签,再截取长度
参考http://blog.csdn.net/gulijiang2008/article/details/7190281中的
//// <summary>
/// 去除HTML标记
/// </summary>
/// <param name="NoHTML">包括HTML的源码 </param>
/// <returns>已经去除后的文字</returns>
public static string NoHTML(string Htmlstring)