在ASP.Net中經常會從網面中取資料或更新網頁的顯示。因為HTML中有些特殊字元如<, >, &等,顯示實際值不一致,造成儲存到資料庫再取出來時會不一樣。因此需要以下函數做轉換:
///<summary>
///替換html中的特殊字元
///</summary>
///<paramname="theString">需要進行替換的文本。</param>
///<returns>替換完的文本。</returns>
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace("\"",""");
theString = theString.Replace("\'", "'");
theString=theString.Replace("\n","<br/>");
return theString;
}
///<summary>
///恢複html中的特殊字元
///</summary>
///<paramname="theString">需要恢複的文本。</param>
///<returns>恢複好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","\"");
theString = theString.Replace("'", "\'");
theString=theString.Replace("<br/>","\n");
return theString;
}
在ASP.Net中經常會從網面中取資料或更新網頁的顯示。因為HTML中有些特殊字元如<, >, &等,顯示實際值不一致,造成儲存到資料庫再取出來時會不一樣。因此需要以下函數做轉換:
///<summary>
///替換html中的特殊字元
///</summary>
///<paramname="theString">需要進行替換的文本。</param>
///<returns>替換完的文本。</returns>
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace("\"",""");
theString = theString.Replace("\'", "'");
theString=theString.Replace("\n","<br/>");
return theString;
}
///<summary>
///恢複html中的特殊字元
///</summary>
///<paramname="theString">需要恢複的文本。</param>
///<returns>恢複好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","\"");
theString = theString.Replace("'", "\'");
theString=theString.Replace("<br/>","\n");
return theString;
}