过滤asp.net过滤html标签只保留换行与空格的方法

html空格  时间:2021-02-27  阅读:()

asp.net过滤html标签只保留换行与空格的方法本文实例讲述了asp.net过滤html标签只保留换行与空格的方法。分享给大家供大家参考。具体分析如下

自己从网上找了一个过滤html标签的方法我也不知道谁的才是原创的反正很多都一样。我把那方法复制下来代码如下

复制代码代码如下:///summary

///去除html标记

////summary

///param name=nohtml包括html的源码/param

///returns已经去除后的文字/returnspubl ic statregex.replace(htmlstring,@([\r\n])[\s]+, ,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\n, );htmlstring=httpcontext.current.server.htmlencode(htmlstring).trim();return htmlstring;

}

以上代码是从网上直接复制过来的这个确实能过滤掉所有的html标签但是这个不是我想要的这个过滤得太干净了我如果用t exta rea输入框的话我是要保留空格跟换行的。然后我就自己改了一下这个方法 textarea的换行是\n所以我得把这些标签重新匹配替换

成br这样的话从数据库中读取到页面时就能正确的换行了把空格替换成html的空格符大功告成。

复制代码代码如下:///summary

///去除html标记(保留br跟\r\n)

////summary

///param name=nohtml包括html的源码/param

///returns已经去除后的文字/returnspubl ic static string newnohtml(string htmlstring)

{

//htmlstring.replace(, %r%n).replace(br= 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\n, );htmlstring=httpcontext.current.server.htmlencode(htmlstring);htmlstring=regex.replace(htmlstring,@((\r\n)),br);htmlstring=regex.replace(htmlstring,@(\r|\n),br);htmlstring=regex.replace(htmlstring,@(\s), );return htmlstring;

}

这个过滤可以用于让用户输入发布内容时的过滤。

希望本文所述对大家的asp.net程序设计有所帮助。

pia云低至20/月,七折美国服务器

Pia云是一家2018的开办的国人商家,原名叫哔哔云,目前整合到了魔方云平台上,商家主要销售VPS服务,采用KVM虚拟架构 ,机房有美国洛杉矶、中国香港和深圳地区,洛杉矶为crea机房,三网回程CN2 GIA,带20G防御,常看我测评的朋友应该知道,一般带防御去程都是骨干线路,香港的线路也是CN2直连大陆,目前商家重新开业,价格非常美丽,性价比较非常高,有需要的朋友可以关注一下。活动方案...

香港ceranetworks(69元/月) 2核2G 50G硬盘 20M 50M 100M 不限流量

香港ceranetworks提速啦是成立于2012年的十分老牌的一个商家这次给大家评测的是 香港ceranetworks 8核16G 100M 这款产品 提速啦老板真的是豪气每次都给高配我测试 不像别的商家每次就给1核1G,废话不多说开始跑脚本。香港ceranetworks 2核2G 50G硬盘20M 69元/月30M 99元/月50M 219元/月100M 519元/月香港ceranetwork...

LOCVPS新上日本软银线路VPS,原生IP,8折优惠促销

LOCVPS在农历新年之后新上架了日本大阪机房软银线路VPS主机,基于KVM架构,配备原生IP,适用全场8折优惠码,最低2GB内存套餐优惠后每月仅76元起。LOCVPS是一家成立于2012年的国人VPS服务商,提供中国香港、韩国、美国、日本、新加坡、德国、荷兰、俄罗斯等地区VPS服务器,基于KVM或XEN架构(推荐选择KVM),线路方面均选择国内直连或优化方案,访问延迟低,适合建站或远程办公使用。...

html空格为你推荐
在线漏洞检测如何查看网站的漏洞?天天酷跑刷金币天天酷跑如何刷分刷金币?神雕侠侣礼包大全神雕侠侣手游版四重大礼包怎么得到啊?创维云电视功能什么是创维云电视啊?创维云电视是什么意思?iphone6上市时间苹果6是什么时候出的 ?分词技术中文分词的应用QzongQZONG怎么玩?网站营运网站运营都分为哪些方面微信怎么看聊天记录什么方法可以知道微信的聊天记录怎样申请支付宝怎么申请支付宝
免费美国主机 美国网站空间 青岛虚拟主机 3322免费域名 已经备案域名 lamp westhost 新加坡服务器 美元争夺战 512m 国外免费空间 小米数据库 vip购优汇 idc资讯 爱奇艺vip免费试用7天 如何安装服务器系统 架设邮件服务器 沈阳主机托管 cxz 金主 更多