zoukankan      html  css  js  c++  java
  • Asp.Net 清除Html标签

    /// <summary>
    /// 清除Html标签
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    protected string ClearHtmlTag(string htmlString)
    {
    //删除脚本
    htmlString = Regex.Replace(htmlString, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
    //删除Html
    htmlString = Regex.Replace(htmlString, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
    htmlString = Regex.Replace(htmlString, @"([ ])[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, @"&(ldquo);", "“", RegexOptions.IgnoreCase);
    htmlString = Regex.Replace(htmlString, @"&(rdquo);", "”", RegexOptions.IgnoreCase);
    htmlString = Regex.Replace(htmlString, @"&#(d+);", "", RegexOptions.IgnoreCase);


    htmlString.Replace("<", "");
    htmlString.Replace(">", "");
    htmlString.Replace(" ", "");

    htmlString = HttpContext.Current.Server.HtmlEncode(htmlString).Trim();

    return htmlString;
    }

  • 相关阅读:
    react条件渲染
    js用replaceAll全部替换的方法
    批量重命名图片,去除括号
    [转]自定义alert弹框,title不显示域名
    [转]ASCII码表及扩展ASCII码表,方便查阅
    [转]PHP中file_put_contents追加和换行
    Response
    Redirect
    Request
    Paginator
  • 原文地址:https://www.cnblogs.com/xinboqinheng/p/3823661.html
Copyright © 2011-2022 走看看