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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cloudbric
Cloudbric
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
S
Secure Thoughts
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
Attack and Defense Labs
Attack and Defense Labs
T
The Blog of Author Tim Ferriss
Vercel News
Vercel News
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
T
Tailwind CSS Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
博客园 - Franky
I
InfoQ
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Help Net Security
Help Net Security
M
MIT News - Artificial intelligence
GbyAI
GbyAI
B
Blog
K
Kaspersky official blog
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
O
OpenAI News
A
About on SuperTechFans
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
CXSECURITY Database RSS Feed - CXSecurity.com
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News

博客园 - 牵牛望岳

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的