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程序设计有所帮助。
BGP.TO目前针对日本和新加坡服务器进行促销,其中日本东京服务器6.5折,而新加坡服务器7.5折起。这是一家专门的独立服务器租售网站,提供包括中国香港、日本、新加坡和洛杉矶的服务器租用业务,基本上都是自有硬件、IP资源等,国内优化直连线路,机器自动化部署上架,并提供产品的基本管理功能(自助开关机重启重装等)。新加坡服务器 $93.75/月CPU:E3-1230v3内存:16GB硬盘:480GB ...
柚子互联官网商家介绍柚子互联(www.19vps.cn)本次给大家带来了盛夏促销活动,本次推出的活动是湖北十堰高防产品,这次老板也人狠话不多丢了一个6.5折优惠券而且还是续费同价,稳撸。喜欢的朋友可以看看下面的活动详情介绍,自从站长这么久以来柚子互联从19年开始算是老商家了。六五折优惠码:6kfUGl07活动截止时间:2021年9月30日客服QQ:207781983本次仅推荐部分套餐,更多套餐可进...
hostslim美国独立日活动正在进行中,针对一款大硬盘荷兰专用服务器:双E5-2620v2/4x 1TB SATA硬盘,活动价60美元月。HostSlim荷兰服务器允许大人内容,不过只支持电汇、信用卡和比特币付款,商家支持7天内退款保证,有需要欧洲服务器的可以入手试试,记得注册的时候选择中国,这样不用交20%的税。hostslim怎么样?HostSlim是一家成立于2008年的荷兰托管服务器商,...