tempad

C#过滤HTML代码

ReadyGo!技术成就梦想 网络搜索 efish 2008-8-2 23:35:20
   public string NoHTML(string Htmlstring)

        //去除HTML标记

           {

                //删除脚本

                    Htmlstring = Regex.Replace(Htmlstring,@"<script*?>.*?</script>","",RegexOptions.IgnoreCase);

                //删除HTML

                    Htmlstring = Regex.Replace(Htmlstring,@"<(.*)>","",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"()+","",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"<!--.*","",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(quot|#34);","\"",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(amp|#38);","&",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(lt|#60);","<",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(gt|#62);",">",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(nbsp|#160);"," ",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(iexcl|#161);","\xa1",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(cent|#162);","\xa2",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(pound|#163);","\xa3",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&(copy|#169);","\xa9",RegexOptions.IgnoreCase);

                    Htmlstring = Regex.Replace(Htmlstring,@"&#(\d+);","",RegexOptions.IgnoreCase);

                    Htmlstring.Replace("<","");

                    Htmlstring.Replace(">","");

                    Htmlstring.Replace("\r","");

                    Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

           return Htmlstring;

           }

责任编辑: efish 参与评论 查找更多:
相关文章
C#中web.config文件详解 C#中web.config文件详解
C#创建Windows服务 C#创建Windows服务
C#数字金额转换大写金额 C#数字金额转换大写金额
c# 启动本机程序 c# 启动本机程序
c# 实现远程关机 c# 实现远程关机
C#创建不规则窗体代码 C#创建不规则窗体代码
如何为程式码加上行号 如何为程式码加上行号
ViewState使用兼谈序列化 ViewState使用兼谈序列化
微软CFO:10亿美元收购雅虎搜索 微软CFO:10亿美元收购雅虎搜索
百度首页添加hao123链接 百度首页添加hao123链接
2秒记住本站域名

玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn

分类导航
ReadyGo!技术成就梦想