当前位置: 代码迷 >> ASP.NET >> asp.net 去除html标记 ,
  详细解决方案

asp.net 去除html标记 ,

热度:4670   发布时间:2013-02-25 00:00:00.0
asp.net 去除html标记 在线等,急急!!!
我在编辑内空时用FCKeditor文本编辑器把内空插入到数据库,我现在想把数据从数据库里读出来,在页面进行绑定数据时只显示文字并截取字符串该怎么做,求代码

------解决方案--------------------------------------------------------
C# code
 public static string DelHtml(this string Htmlstring)    {        //删除脚本           Htmlstring = Regex.Replace(Htmlstring, @"<script(\s[^>]*?)?>[\s\S]*?</script>", "", RegexOptions.IgnoreCase);        //删除样式          Htmlstring = Regex.Replace(Htmlstring, @"<style>[\s\S]*?</style>", "", RegexOptions.IgnoreCase);        //删除html标签        Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);        return Htmlstring;    }
------解决方案--------------------------------------------------------
你把< 和 > 等关键字符转义一下就行了.
比如 < , 转义成 &lt; 存到数据库,读取的时候, &lt; 就会自动转义成 < 了.
------解决方案--------------------------------------------------------
C# code
/// <summary>        /// HTML标签过滤        /// </summary>        /// <param name="strHtml">需要过滤的字符串</param>        /// <returns></returns>        public static string StripHTML(string strHtml)        {            string[] aryReg ={                                  @"<script[^>]*?>.*?</script>",                                  @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",                                  @"([\r\n])[\s]+",                                  @"&(quot|#34);",                                  @"&(amp|#38);",                                  @"&(lt|#60);",                                  @"&(gt|#62);",                                   @"&(nbsp|#160);",                                   @"&(iexcl|#161);",                                  @"&(cent|#162);",                                  @"&(pound|#163);",                                  @"&(copy|#169);",                                  @"&#(\d+);",                                  @"-->",                                  @"<!--.*\n"                              };            string[] aryRep = {                                   "",                                   "",                                   "",                                   "\"",                                   "&",                                   "<",                                   ">",                                   " ",                                   "\xa1",//chr(161),                                   "\xa2",//chr(162),                                   "\xa3",//chr(163),                                   "\xa9",//chr(169),                                   "",                                   "\r\n",                                   ""                               };            string newReg = aryReg[0];            string strOutput = strHtml;            for (int i = 0; i < aryReg.Length; i++)            {                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);                strOutput = regex.Replace(strOutput, aryRep[i]);            }            strOutput.Replace("<", "");            strOutput.Replace(">", "");            strOutput = strOutput.Replace("\r\n", "");            return strOutput;        }
------解决方案--------------------------------------------------------
  相关解决方案