点关注不迷路

Max

.Net中富文本提取文字

public static string StripHTML(string stringToStrip)
        {
            if (!string.IsNullOrEmpty(stringToStrip))
            {
                stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", "\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                stringToStrip = Regex.Replace(stringToStrip, "<br(?:\\s*)/>", "\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                stringToStrip = Regex.Replace(stringToStrip, "\"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                stringToStrip = StripHtmlXmlTags(stringToStrip);
            }
            return stringToStrip;
        }
        private static string StripHtmlXmlTags(string content)
        {
            return Regex.Replace(content, "<[^>]+>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }
posted @ 2021-06-22 14:30  Max麦克斯  阅读(235)  评论(0编辑  收藏  举报