读书人

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

发布时间: 2012-01-06 22:55:18 作者: rapoo

如何将HTML上的转义字符直接转成HTML格式的元素
我如何将下列已经转义好的文本变成HTML格式,比如<编程<形式,并以HTML可视元素形式在页面上显示
代码:
<script language= "javascript ">
function Post01()
{

document.getElementById( "TextBox1 ").value= "<table width= "100% " cellpadding= "0 " cellspacing= "10 " ><tr><td height= "25 " colspan= "2 " class= "title1 "><span>优衣日志</span></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "1 "><img alt= "张杰的日志1 " src=1 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-15 0:00:00] </span><a href= "6 ">张杰1</a> 的 <a href= "1 ">张杰的日志1</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容1<a href= "1 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "6 "><img alt= "张杰的日志1 " src=6 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-15 0:00:00] </span><a href= "6 ">张杰1</a> 的 <a href= "6 ">张杰的日志1</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容1<a href= "6 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "14 "><img alt= "张杰的日志1 " src=14 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-15 0:00:00] </span><a href= "6 ">张杰1</a> 的 <a href= "14 ">张杰的日志1</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容1<a href= "14 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "15 "><img alt= "张杰的日志2 " src=15 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-14 0:00:00] </span><a href= "6 ">张杰2</a> 的 <a href= "15 ">张杰的日志2</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容2<a href= "15 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "7 "><img alt= "张杰的日志2 " src=7 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-14 0:00:00] </span><a href= "6 ">张杰2</a> 的 <a href= "7 ">张杰的日志2</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容2<a href= "7 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "3 "><img alt= "张杰的日志2 " src=3 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-14 0:00:00] </span><a href= "6 ">张杰2</a> 的 <a href= "3 ">张杰的日志2</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容2<a href= "3 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "4 "><img alt= "张杰的日志3 " src=4 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-13 0:00:00] </span><a href= "6 ">张杰3</a> 的 <a href= "4 ">张杰的日志3</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容3<a href= "4 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "8 "><img alt= "张杰的日志3 " src=8 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-13 0:00:00] </span><a href= "6 ">张杰3</a> 的 <a href= "8 ">张杰的日志3</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容3<a href= "8 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "16 "><img alt= "张杰的日志3 " src=16 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-13 0:00:00] </span><a href= "6 ">张杰3</a> 的 <a href= "16 ">张杰的日志3</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容3<a href= "16 ">[查看]</a></td></tr><tr><td width= "100 " height= "100 " align= "center " valign= "middle " class= "b100 "><a href= "12 "><img alt= "张杰的日志3 " src=12 " /></a></td><td width= "630 " class= "td1 "><span class= "myright ">[ 2007-4-13 0:00:00] </span><a href= "6 ">张杰3</a> 的 <a href= "12 ">张杰的日志3</a><br /><span class= "highlight ">所属分类:</span><a href= "# ">购物经历</a> <span class= "highlight ">标签:</span><br /><span class= "highlight ">日志内容:</span>张杰的日志内容3<a href= "12 ">[查看]</a></td></tr><tr ><td colspan= "2 "><div class= "myright ">1 | <a href= "# " >2</a> | <a href= "# " >下一页</a></div></td></tr></table> ";


}
</script>
<body onload= "Post01(); ">
<form id= "form1 " runat= "server ">
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " Width= "70px " />   <br />
<asp:TextBox ID= "TextBox1 " runat= "server " Height= "549px " TextMode= "MultiLine " Width= "704px "> </asp:TextBox>
<div id= "div1 "> </div>
<div id= "div2 "> </div>
</form>
</body>

[解决办法]
//这几个函数 对你应该有用

/// <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;
}
[解决办法]
Replace 必须使用循环

string strHTml = " <> <> <> ";
While(strHTml.IndexOf( ' < ') > =0 )
{
strHTml = strHTml.Replace( " < ", " ");
}
------解决方案--------------------


用正则表达式,
function Post01()
{
var reg= /^[ <]$/g;
var str = document.getElementById( "TextBox1 ").value;
var newstr = str.replace(reg, "< ");
}
> 这个符号类似

读书人网 >asp.net

热点推荐