现在的位置: 首页 > 综合 > 正文

去除字符串中的html标签

2012年10月17日 ⁄ 综合 ⁄ 共 1161字 ⁄ 字号 评论关闭

        /// <summary>
        /// 将Html标签转化为空 贾世义
        /// </summary>
        /// <param name="strHtml">待转化的字符串</param>
        /// <returns>经过转化的字符串</returns>
        public static string GetStringNoHtml(string strHtml)
        {
            if (String.IsNullOrEmpty(strHtml))
            {
                return strHtml;
            }
            else
            {
                string[] aryReg ={ 
                @"<script[^>]*?>.*?</script>", 
                @"<!--.*\n(-->)?", 
                @"<(\/\s*)?(.|\n)*?(\/\s*)?>", 
                @"<(\w|\s|""|'| |=|\\|\.|\/|#)*", 
                @"([\r\n|\s])*", 
                @"&(quot|#34);", 
                @"&(amp|#38);", 
                @"&(lt|#60);", 
                @"&(gt|#62);", 
                @"&(nbsp|#160);", 
                @"&(iexcl|#161);", 
                @"&(cent|#162);", 
                @"&(pound|#163);", 
                @"&(copy|#169);", 
                @"&#(\d+);"};

                string newReg = aryReg[0];
                string strOutput = strHtml.Replace("&nbsp;", " ");
                for (int i = 0; i < aryReg.Length; i++)
                {
                    Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                    strOutput = regex.Replace(strOutput, "");
                }
                strOutput.Replace("<", "&gt;");
                strOutput.Replace(">", "&lt;");
                return strOutput.Replace(" ", "&nbsp;");
            }

        }

欢迎访问:http://121.18.78.216 适易查询分析、工作流、内容管理及项目管理演示平台

抱歉!评论已关闭.