天天看点

删除提交文本和显示文本HTML标签

在现实用户提交的文本信息时,有时因为信息显示不下需要截断,但是因为html标签的存在导致统计字数在显示时不正确。

  一、将显示文本的所有html标签删除

    public string delHTMLTag(string str)

   {

       return Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

    }

二、删除不必要的html标签,其他标签保留

     html=html.Replace("<(?i)script(.*?)/(.*?)>", "")

      .Replace("<(?i)iframe(.*?)/(.*?)>", "")

      .Replace("<(?i)link(.*?)/(.*?)>", "")

      .Replace("<(?i)head(.*?)/>", "")

      .Replace("<(?i)/head>", "")

      .Replace("<(?i)head />", "")

      .Replace("<(?i)body>", "")

      .Replace("<(?i)/body>","");