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云是一家2018的开办的国人商家,原名叫哔哔云,目前整合到了魔方云平台上,商家主要销售VPS服务,采用KVM虚拟架构 ,机房有美国洛杉矶、中国香港和深圳地区,洛杉矶为crea机房,三网回程CN2 GIA,带20G防御,常看我测评的朋友应该知道,一般带防御去程都是骨干线路,香港的线路也是CN2直连大陆,目前商家重新开业,价格非常美丽,性价比较非常高,有需要的朋友可以关注一下。活动方案...
香港ceranetworks提速啦是成立于2012年的十分老牌的一个商家这次给大家评测的是 香港ceranetworks 8核16G 100M 这款产品 提速啦老板真的是豪气每次都给高配我测试 不像别的商家每次就给1核1G,废话不多说开始跑脚本。香港ceranetworks 2核2G 50G硬盘20M 69元/月30M 99元/月50M 219元/月100M 519元/月香港ceranetwork...
LOCVPS在农历新年之后新上架了日本大阪机房软银线路VPS主机,基于KVM架构,配备原生IP,适用全场8折优惠码,最低2GB内存套餐优惠后每月仅76元起。LOCVPS是一家成立于2012年的国人VPS服务商,提供中国香港、韩国、美国、日本、新加坡、德国、荷兰、俄罗斯等地区VPS服务器,基于KVM或XEN架构(推荐选择KVM),线路方面均选择国内直连或优化方案,访问延迟低,适合建站或远程办公使用。...