參照petshop 4.0
/// <summary> /// 此方法用于確認(rèn)用戶輸入的不是惡意信息 /// </summary> /// <param name="text">用戶輸入信息</param> /// <param name="maxLength">輸入的最大長度</param> /// <returns>處理后的輸入信息</returns> public static string InputText(string text, int maxLength) { text = text.Trim(); if (string.IsNullOrEmpty(text)) return string.Empty; if (text.Length > maxLength) text = text.Substring(0, maxLength); //將網(wǎng)頁中非法和有攻擊性的符號(hào)替換掉,以防sql注入!返回正常數(shù)據(jù) text = Regex.Replace(text, "[\\s]{2,}", " "); // 2個(gè)或以上的空格 text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br> html換行符 text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // html空格符 text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); // 任何其他的標(biāo)簽 text = text.Replace("‘", "‘‘");// 單引號(hào) return text; } |
|