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

推荐订阅源

D
DataBreaches.Net
罗磊的独立博客
雷峰网
雷峰网
量子位
V
Visual Studio Blog
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
月光博客
月光博客
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
腾讯CDC
Engineering at Meta
Engineering at Meta
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
Jina AI
Jina AI
A
About on SuperTechFans

博客园 - 牵牛望岳

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的