转换 Html 内容为纯文本内容(html,文本互转)

        /// <summary>
        /// 转换纯文本内容为 HTML 内容
      /// </summary>
        /// <param name="Text">纯文本内容</param>
        /// <returns>转换后的 HTML 内容</returns>
        public static string ToHTML(string Text)
        {
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace("<p>" + Text + "</p>", "\r\n", "</p><p>"), "\r", "</p><p>"), "\n", "<br />"), "\t", "    "), "  ", "  ");
        }
        /// <summary>
        /// 转换 Html 内容为纯文本内容
      /// </summary>
        /// <param name="HTML">HTML 内容</param>
        /// <returns>转换后的纯文本内容</returns>
        public static string ToText(string HTML)
        {
            string input = HTML;
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(input, @"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(?m)<select[^>]*>(\w|\W)*?</select[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(?m)<a[^>]*>(\w|\W)*?</a[^>]*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), "(<[^>]+?>)| ", "", RegexOptions.Multiline | RegexOptions.IgnoreCase), @"(\s)+", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
        }

posted @ 2011-08-09 20:06  .net|C#|js|jquery|css|div|html  阅读(18371)  评论(1编辑  收藏  举报