惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
博客园_首页
WordPress大学
WordPress大学
博客园 - 聂微东
P
Privacy International News Feed
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
NISL@THU
NISL@THU
美团技术团队
T
Tailwind CSS Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Hacker News
The Hacker News
B
Blog
P
Palo Alto Networks Blog
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
The Register - Security
The Register - Security
S
Securelist
A
Arctic Wolf
MyScale Blog
MyScale Blog
H
Help Net Security
N
Netflix TechBlog - Medium
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Latest
Security Latest
T
Tor Project blog
V
Vulnerabilities – Threatpost
V
V2EX
AI
AI
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Heimdal Security Blog
Google Online Security Blog
Google Online Security Blog
Know Your Adversary
Know Your Adversary

博客园 - 牵牛望岳

ICloneable 接口--c# 深复制与浅复制 IDisposable接口详解 C# 中用 PadLeft、PadRight 补足位数 listbox控件的一些操作 IsPostBack用法,Page.IsValid的用法 IIf函数 C#连接数据库的一些鲜为人知的方法 C#关键字 【推荐】常用 SQL 语句大全 CSS中的黄金分割率 几条相关SQL语句 解析.net中ref和out的实质 JavaScript substr() 和 substring() 方法的区别 武林外传搞笑语录 调试和跟踪 VS2005单元测试[转] 几个.Net开源的CMS、Portal系统 DataKeyNames工作 查询后,翻页问题的解决办法2。(GridView1.PageIndex = e.NewPageIndex;) (转)
替换、恢复Html中的特殊字符
牵牛望岳 · 2009-04-14 · via 博客园 - 牵牛望岳

public static string HtmlEncode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "&lt;");
theString = theString.Replace(" ", " &nbsp;");
theString = theString.Replace(" ", " &nbsp;");
theString = theString.Replace("\"", "&quot;");
theString = theString.Replace("\'", "&#39;");
theString = theString.Replace("\n", "<br/> ");
return theString;
}
第一段是转换textarea里面的特殊字符的,用处是:比如在做网页的时候,提供了一个textarea框给用户,让用户输入一个自我简介什么的,然后保存到数据库里,在保存到库里以前要做一下这样的转换,为了安全考虑,还有就是特殊字符这样转换后才能保留,不然在textarea里敲空格和回车,在保存的时候,这些是没有了的,输出到网页上会发现,什么格式都没有了。
public static string HtmlDiscode(string theString)
{
theString = theString.Replace("&gt;", ">");
theString = theString.Replace("&lt;", "<");
theString = theString.Replace("&nbsp;", " ");
theString = theString.Replace(" &nbsp;", " ");
theString = theString.Replace("&quot;", "\"");
theString = theString.Replace("&#39;", "\'");
theString = theString.Replace("<br/> ", "\n");
return theString;
}
这段段代码正好与第一段代码相反,比如,用户登录后,要修改个人简介,你要把你保存的个人简介放入到textarea里,就必须再做一次反向转换,不然会显示一些不是用户输入的字符。

public static string DealHtml(string str)
{
str = Regex.Replace(str, @"\<(img)[^>]*>|<\/(img)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<(table|tbody|tr|td|th|)[^>]*>|<\/(table|tbody|tr|td|th|)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<(div|blockquote|fieldset|legend)[^>]*>|<\/(div|blockquote|fieldset|legend)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<(font|i|u|h[1-9]|s)[^>]*>|<\/(font|i|u|h[1-9]|s)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<(style|strong)[^>]*>|<\/(style|strong)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\<a[^>]*", "", RegexOptions.IgnoreCase);
return st
这段代码是用正则表达式过滤html标记的,它提到的html标记都会被过滤掉
比如在发表文章的时候,用的是html在线编辑器,这样会让文章内容包含html代码,如果这时候,你想到网站首页上显示一部分文章内容,这个时间如果你直接截取文章内容,可能会把包含的html代码截断开,这样在首页上显示的html代码不全,会导致出现一些破碎的html代码,我们一般的做法就是把html代码过滤掉,只剩文字,这样显示出来再用css格式化,在首页上,就好看多了,这个函数,就是把str过滤成纯净的文字,不包含html的