/********************************************** * 類作用: HTML格式輔助類 * 作者:開優網絡 * http://www.568387.com ***********************************************/ using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace Svnhost.Common { public class HtmlUtils { /// <summary> /// 替換回車換行符為html換行符 /// </summary> public static string StrFormat(string str) { string str2; if (str == null) { str2 = ""; } else { str = str.Replace("\r\n", "<br />"); str = str.Replace("\n", "<br />"); str2 = str; } return str2; } public static string GetRealIP() { string ip = Request.GetIP(); return ip; } /// <summary> /// 改正sql語句中的轉義字符 /// </summary> public static string mashSQL(string str) { string str2; if (str == null) { str2 = ""; } else { str = str.Replace("\'", "'"); str2 = str; } return str2; } /// <summary> /// 替換sql語句中的有問題符號 /// </summary> public static string ChkSQL(string str) { string str2; if (str == null) { str2 = ""; } else { str = str.Replace("'", "''"); str2 = str; } return str2; } /// <summary> /// 替換html字符 /// </summary> public static string EncodeHtml(string strHtml) { if (strHtml != "") { strHtml = strHtml.Replace(",", "&def"); strHtml = strHtml.Replace("'", "&dot"); strHtml = strHtml.Replace(";", "&dec"); return strHtml; } return ""; } /// <summary> /// 為腳本替換特殊字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ReplaceStrToScript(string str) { str = str.Replace("\\", "\\\\"); str = str.Replace("'", "\\'"); str = str.Replace("\"", "\\\""); return str; } /// <summary> /// 移除Html標記 /// </summary> /// <param name="content"></param> /// <returns></returns> public static string RemoveHtml(string content) { string regexstr = @"<[^>]*>"; return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase); } /// <summary> /// 過濾HTML中的不安全標簽 /// </summary> /// <param name="content"></param> /// <returns></returns> public static string RemoveUnsafeHtml(string content) { content = Regex.Replace(content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase); content = Regex.Replace(content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase); return content; } /// <summary> /// 從HTML中獲取文本,保留br,p,img /// </summary> /// <param name="HTML"></param> /// <returns></returns> public static string GetTextFromHTML(string HTML) { System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); return regEx.Replace(HTML, ""); } } }