怎么将HTML上的转义字符直接转成HTML格式的元素

 我来答
HoLee1997
2015-01-28 · TA获得超过1961个赞
知道大有可为答主
回答量:2557
采纳率:84%
帮助的人:967万
展开全部
HTML有转义字符?HTML应该就是标签语言。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友96ffcf7
推荐于2016-04-06 · 知道合伙人互联网行家
百度网友96ffcf7
知道合伙人互联网行家
采纳数:22721 获赞数:118724
从事多年网络方面工作,有丰富的互联网经验。

向TA提问 私信TA
展开全部
//这几个函数 对你应该有用

/// <summary>
/// 过滤SQL/Html
/// </summary>
/// <param name= "text "> </param>
/// <returns> </returns>
public static string FilterSQL(string text)
{
string validSql = " ";
if (text != null)
{
text = text.Replace( "\ " ", "" ");
//text = text.Replace( "; ", " ' '; ' ' ");
//text = text.Replace( " ' ", " ' ' ");
//text = text.Replace( "-- ", " ' '-- ' ' ");

//text = text.Replace( "%25 ", " ");
//text = text.Replace( "%0a ", " ");
//text = text.Replace( "%22 ", " ");
//text = text.Replace( "%27 ", " ");
//text = text.Replace( "%5c ", " ");
//text = text.Replace( "%2f ", " ");
//text = text.Replace( "%3c ", " ");
//text = text.Replace( "%3e ", " ");
//text = text.Replace( "%26 ", " ");

text = text.Replace( " < ", "< ");
text = text.Replace( "> ", "> ");
validSql = text;
}
return validSql;
}

public static string FilterTestHtml(string text)
{
string validSql = " ";
if (text != null)
{
text = text.Replace( " ", " ");
text = text.Replace( "& ", "& ");
text = text.Replace( "\n ", " <br> ");
//text = text.Replace( "\n ", " <br> ");
//text = text.Replace( "\r ", " <br> ");
//text = text.Replace( "\ " ", "" ");
//text = text.Replace( "; ", " ' '; ' ' ");

//text = text.Replace( "-- ", " ' '-- ' ' ");
//text = text.Replace( "-- ", " ' '-- ' ' ");
//text = text.Replace( " < ", "< ");
// = text.Replace( "> ", "> ");
validSql = text;
}
return validSql;
}
public static string UnFilterTestHtml(string text)
{
string validSql = " ";
if (text != null)
{
text = text.Replace( " ", " ");
text = text.Replace( " <br> ", "\n ");
//text = text.Replace( "> ", "> ");
validSql = text;
}
return validSql;
}
[解决办法]
/// <summary>
/// 去除HTML标记
/// </summary>
/// <param name= "NoHTML "> 包括HTML的源码 </param>
/// <returns> 已经去除后的文字 </returns>
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring,@ " <script[^> ]*?> .*? </script> ", " ",RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring,@ " <(.[^> ]*)> ", " ",RegexOptions.IgnoreCase);
Htmlstring = Regex.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;
}

本文来自:读书人网(http://www.reader8.cn/)原文链接:http://www.reader8.cn/jiaocheng/20120106/1623897.html
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
liwsh911
2015-01-28 · TA获得超过117个赞
知道答主
回答量:274
采纳率:0%
帮助的人:72.4万
展开全部
举个例子呗
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式