怎么将HTML上的转义字符直接转成HTML格式的元素
3个回答
展开全部
HTML有转义字符?HTML应该就是标签语言。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-04-06 · 知道合伙人互联网行家
关注
展开全部
//这几个函数 对你应该有用
/// <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
/// <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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
举个例子呗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询