替换、恢复Html中的特殊字符

时间:2022-11-02 17:18:10

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的