博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

html过滤操作方法

Posted on 2010-10-21 13:32  一刻  阅读(197)  评论(0)    收藏  举报

 /// <summary>
    /// 替换html中的特殊字符
    /// </summary>
    /// <param name="theString">需要进行替换的文本。</param>
    /// <returns>替换完的文本。</returns>
    public static string HtmlEncode(string theString)
    {
        theString = theString.Replace(">", "&gt;");
        theString = theString.Replace("<", "&lt;");
        theString = theString.Replace("  ", " &nbsp;");
        theString = theString.Replace("  ", " &nbsp;");
        theString = theString.Replace("\"", "&quot;");
        theString = theString.Replace("\'", "&#39;");
        theString = theString.Replace("\n", "<br/> ");
        return theString;
    }

    /// <summary>
    /// 恢复html中的特殊字符
    /// </summary>
    /// <param name="theString">需要恢复的文本。</param>
    /// <returns>恢复好的文本。</returns>
    public static string HtmlDiscode(string theString)
    {
        theString = theString.Replace("&gt;", ">");
        theString = theString.Replace("&lt;", "<");
        theString = theString.Replace("&nbsp;", " ");
        theString = theString.Replace(" &nbsp;", "  ");
        theString = theString.Replace("&quot;", "\"");
        theString = theString.Replace("&#39;", "\'");
        theString = theString.Replace("<br/> ", "\n");
        return theString;
    }

    public static string DealHtml(string str)
    {
        str = Regex.Replace(str, @"\<(img)[^>]*>|<\/(img)>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<(table|tbody|tr|td|th|)[^>]*>|<\/(table|tbody|tr|td|th|)>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<(div|blockquote|fieldset|legend)[^>]*>|<\/(div|blockquote|fieldset|legend)>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<(font|i|u|h[1-9]|s)[^>]*>|<\/(font|i|u|h[1-9]|s)>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<(style|strong)[^>]*>|<\/(style|strong)>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"\<a[^>]*", "", RegexOptions.IgnoreCase);
        return str;
    }