//去掉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标签,再截取长度
参考http://blog.csdn.net/gulijiang2008/article/details/7190281中的
//// <summary>
/// 去除HTML标记
/// </summary>
/// <param name="NoHTML">包括HTML的源码 </param>
/// <returns>已经去除后的文字</returns>
public static string NoHTML(string Htmlstring)
不知道怎么做了,我是初学者。
@缘分0919: 先把给你参考的NoHTML方法copy到你项目中,然后把你要去掉html的字符串传到这个方法里面,返回的结果就是纯文字了,然后你要显示多少个字就再substring()一下
这种东西一般就是,要么不考虑性能什么的,用正则,简便;要么就找专门的HTML解析程序。。。
考虑用js正则去掉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;