过滤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程序设计有所帮助。

建站选择网站域名和IP主机地址之间关系和注意要点

今天中午的时候有网友联系到在选择网站域名建站和主机的时候问到域名和IP地址有没有关联,或者需要注意的问题。毕竟我们在需要建站的时候,我们需要选择网站域名和主机,而主机有虚拟主机,包括共享和独立IP,同时还有云服务器、独立服务器、站群服务器等形式。通过这篇文章,简单的梳理关于网站域名和IP之间的关系。第一、什么是域名所谓网站域名,就是我们看到的类似"www.laozuo.org",我们可以通过直接记...

注册做什么96%可以干啥,常用的7个常用的国内国外域名注册服务商_云服务器可以干什么

日前,国内知名主机服务商阿里云与国外资深服务器面板Plesk强强联合,推出 阿里云域名注册与备案、服务器ECS购买与登录使用 前言云服务器(Elastic  只需要确定cpu内存与带宽基本上就可以了,对于新手用户来说,我们在购买阿里云服务申请服务器与域名许多云服务商的云服务器配置是弹性的 三周学会小程序第三讲:服务 不过这个国外服务器有点慢,可以考虑国内的ngrokcc。 ngrokcc...

[6.18]IMIDC:香港/台湾服务器月付30美元起,日本/俄罗斯服务器月付49美元起

IMIDC发布了6.18大促销活动,针对香港、台湾、日本和莫斯科独立服务器提供特别优惠价格最低月付30美元起。IMIDC名为彩虹数据(Rainbow Cloud),是一家香港本土运营商,全线产品自营,自有IP网络资源等,提供的产品包括VPS主机、独立服务器、站群独立服务器等,数据中心区域包括香港、日本、台湾、美国和南非等地机房,CN2网络直连到中国大陆。香港服务器   $39/...

html空格为你推荐
苏州商标注册苏州商标注册怎么办理cornerradius怎么用代码写一个圆角矩形?博客外链博客外链怎么做好安卓应用平台app应用平台有哪些 应用平台哪些怎么点亮qq空间图标QQ空间的图标怎么点亮创维云电视功能很喜欢创维云电视,它到底有哪些独特功能?分词技术中文分词的应用声母是什么什么是声母,什么是音母?怎样申请支付宝如何申请支付宝?怎样申请支付宝怎么申请支付宝
北京虚拟主机租用 photonvps 台湾服务器 linode代购 国外服务器 kddi 老左博客 搜狗抢票助手 申请空间 赞助 腾讯实名认证中心 阿里校园 上海联通宽带测速 ebay注册 免费asp空间 湖南idc 深圳域名 阿里云个人邮箱 密钥索引 SmartAXMT800 更多