c# 过滤html标签

发布日期:2018-02-05    浏览次数:858
 /// <summary>
 /// 过滤html标签
 /// </summary>
 /// <param name="strHtml">html的内容</param>
 /// <returns></returns>
 public static string StripHTML(string stringToStrip)
 {
   // paring using RegEx 

   stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", "\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);

   stringToStrip = Regex.Replace(stringToStrip, "<br(?:\\s*)/>", "\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);

   stringToStrip = Regex.Replace(stringToStrip, """, "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);

   stringToStrip = StripHtmlXmlTags(stringToStrip);

   stringToStrip = stringToStrip.Replace("&nbsp;", "");

   return stringToStrip;

 }
 private static string StripHtmlXmlTags(string content)
 {
   return Regex.Replace(content, "<[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 }

本文网址:https://www.wyxxw.cn/blog-detail-2-6-51.html

返回列表

非特殊说明,本文版权归原作者所有,转载请注明出处

提示:本站所有资源仅供学习与参考,请勿用于商业用途。图片来自互联网~如侵犯您的权益,请联系QQ:1067507709.

提示:转载请注明来自:https://www.wyxxw.cn/blog-detail-2-6-51.html 。 本文发布者:momo