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程序设计有所帮助。
百纵科技:美国云服务器活动重磅来袭,洛杉矶C3机房 带金盾高防,会员后台可自助管理防火墙,添加黑白名单 CC策略开启低中高.CPU全系列E52680v3 DDR4内存 三星固态盘列阵。另有高防清洗!百纵科技官网:https://www.baizon.cn/联系QQ:3005827206美国洛杉矶 CN2 云服务器CPU内存带宽数据盘防御价格活动活动地址1核1G10M10G10G38/月续费同价点击...
昔日数据怎么样?昔日数据新上了湖北十堰云服务器,湖北十堰市IDC数据中心 母鸡采用e5 2651v2 SSD MLC企业硬盘 rdid5阵列为数据护航 100G高防 超出防御峰值空路由2小时 不限制流量。目前,国内湖北十堰云服务器,首月6折火热销售限量30台价格低至22元/月。(注意:之前有个xrhost.cn也叫昔日数据,已经打不开了,一看网站LOGO和名称为同一家,有一定风险,所以尽量不要选择...
imidc怎么样?imidc彩虹数据或彩虹网络现在促销旗下日本多IP站群独立服务器,原价159美元的机器现在只需要88美元,而且给13个独立IPv4,30Mbps直连带宽,不限制月流量!IMIDC又名为彩虹数据,rainbow cloud,香港本土运营商,全线产品都是商家自营的,自有IP网络资源等,提供的产品包括VPS主机、独立服务器、站群独立服务器等,数据中心区域包括香港、日本、台湾、美国和南非...