要求
1:剔除文本中的html标签
2:保留文本中的html换行标签
ps:换行标签可以是 <br/> <br> <br / > <br style="" />等

解决方案 »

  1.   

    1、将换行标签替换成特殊字符串比如$br style="" /$
    2、替换html标签
    3、替换特殊字符串为原来标签
      

  2.   


            /// <summary>
            /// 过滤字符串中的html代码
            /// </summary>
            /// <param name="Str"></param>
            /// <returns>返回过滤之后的字符串</returns>
            public static string LostHTML(string Str)
            {
                string Re_Str = "";
                if (Str != null)
                {
                    if (Str != string.Empty)
                    {
                        string Pattern = "<\\/*[^<>]*>";
                        Re_Str = Regex.Replace(Str, Pattern, "");
                    }
                }
                return (Re_Str.Replace("\\r\\n", "")).Replace("\\r", "");
            }
      

  3.   

    HTML转换 /// <summary>
            /// 插入SQL时替换字符
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string Encode(string str)
            {
                str = str.Replace("'", "''");
                str = str.Replace("\"", "&quot;");
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                str = str.Replace("\n", "<br>");
                str = str.Replace("“", "&ldquo;");
                str = str.Replace("”", "&rdquo;");
                return str;
            }        /// <summary>
            /// 取SQL值时还原字符
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string Decode(string str)
            {
                str = str.Replace("&rdquo;", "”");
                str = str.Replace("&ldquo;", "“");
                str = str.Replace("<br>", "\n");
                str = str.Replace("&gt;", ">");
                str = str.Replace("&lt;", "<");
                str = str.Replace("&quot;", "\"");
                str = str.Replace("''", "'");
                return str;
            }