1 public class FormatStr
2 {
3 #region MD5加密
4 /// <summary>
5 /// MD5加密操作
6 /// </summary>
7 /// <param name="str">加密的字符串</param>
8 /// <returns></returns>
9 public static string MD5(string str) {
10 return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
11 }
12 #endregion
13
14 #region 去除HTML标记
15 /// <summary>
16 /// 去除HTML标记
17 /// </summary>
18 /// <param name="NoHTML">包括HTML的源码 </param>
19 /// <returns>已经去除后的文字</returns>
20 public static string NoHTML(string Htmlstring)
21 {
22 //删除脚本
23 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
24 //删除HTML
25 Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
26 Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
27 Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
28 Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
29
30 Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
31 Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
32 Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
33 Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
34 Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
35 Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
36 Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
37 Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
38 Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
39 Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
40
41 Htmlstring.Replace("<", "");
42 Htmlstring.Replace(">", "");
43 Htmlstring.Replace("\r\n", "");
44 Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
45
46 return Htmlstring;
47 }
48 #endregion
49
50 #region 去除非法字串
51 /// <summary>
52 /// 去除非法字串
53 /// </summary>
54 /// <param name="strChar">原字串</param>
55 /// <returns>过滤过的字串</returns>
56 public static string ReplaceBadChar(string strChar)
57 {
58 if (strChar.Trim() == "")
59 {
60 return "";
61 }
62 else
63 {
64 strChar = strChar.Replace("'", "");
65 strChar = strChar.Replace("*", "");
66 strChar = strChar.Replace("?", "");
67 strChar = strChar.Replace("(", "");
68 strChar = strChar.Replace(")", "");
69 strChar = strChar.Replace("<", "");
70 strChar = strChar.Replace("=", "");
71 return strChar.Trim();
72 }
73 }
74 #endregion
75
76 #region 检察是否都是数字
77 /// <summary>
78 /// 检察是否都是数字
79 /// </summary>
80 /// <param name="str">要检查的字串</param>
81 /// <returns>bool</returns>
82 public static bool IsNumeric(string str)
83 {
84 Regex reg = new Regex(@"^[+]?\d*$");
85 return reg.IsMatch(str);
86 }
87 #endregion
88
89 #region 检察是否正确的Email格式
90 /// <summary>
91 /// 检察是否正确的Email格式
92 /// </summary>
93 /// <param name="str">要检查的字串</param>
94 /// <returns>bool</returns>
95 public static bool IsEmail(string str)
96 {
97 Regex reg = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
98 return reg.IsMatch(str);
99 }
100 #endregion
101
102 #region 检察是否正确的日期格式
103 /// <summary>
104 /// 检察是否正确的日期格式
105 /// </summary>
106 /// <param name="str">要检查的字串</param>
107 /// <returns>bool</returns>
108 public static bool IsDate(string str)
109 {
110 //考虑到了4年一度的366天,还有特殊的2月的日期
111 Regex reg = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
112 return reg.IsMatch(str);
113 }
114 #endregion
115
116 #region HTML转换为字符串
117 /// <summary>
118 /// HTML转换为字符串,转换标识
119 /// </summary>
120 /// <param name="str"></param>
121 /// <returns></returns>
122 public static string htmToStr(string str)
123 {
124 return str.Replace("\'", "mySQ;").Replace("\"", "myDQ;").Replace("<", "myLt;").Replace(">", "myGt;").Replace("-", "myMl;").Replace("_", "myBl;").Replace("%", "myBs;").Replace("?", "myQe;").Replace("*", "myAl;");
125 }
126 #endregion
127
128 #region 字符串转换为HTML
129 /// <summary>
130 /// 字符串转换为HTML,将还原标识
131 /// </summary>
132 /// <param name="str"></param>
133 /// <returns></returns>
134 public static string strToHtm(string str)
135 {
136 if (str == null) return "";
137 return str.Replace("mySQ;", "\'").Replace("myDQ;", "\"").Replace("myLt;", "<").Replace("myGt;", ">").Replace("myMl;", "-").Replace("myBl;", "_").Replace("myBs;", "%").Replace("myQe;", "?").Replace("myAl;", "*");
138 }
139 #endregion
140
141 #region 文本转换为HTML
142 /// <summary>
143 /// 文本转换为HTML,更换(\n)
144 /// </summary>
145 /// <param name="str"></param>
146 /// <returns></returns>
147 public static string textToHtm(string str)
148 {
149 return str.Replace("\t", "").Replace("<", "<").Replace(">", ">").Replace(" ", " ").Replace("\r\n", "<br>").Replace("\n", "<br>");
150 }
151 #endregion
152
153 #region 文本转换为输入框内容
154 /// <summary>
155 /// 文本转换为输入框内容,
156 /// </summary>
157 /// <param name="str"></param>
158 /// <returns></returns>
159 public static string HtmToInputValue(string str)
160 {
161 //return str.Replace("<","<").Replace(">",">").Replace("\"",""").Replace("&","&");
162 return str.Replace("\"", """);
163 }
164 #endregion
165
166 #region 防Sql注入攻击
167 /// <summary>
168 /// 防Sql注入攻击
169 /// </summary>
170 /// <param name="strQueryName"></param>
171 /// <returns></returns>
172 public static string getQueryString(string strQueryName)
173 {
174 //(')(--)(or )(=)
175 System.Web.HttpRequest myRequest = System.Web.HttpContext.Current.Request;
176 string str1 = myRequest.QueryString[strQueryName];
177 if (str1 == null) return "";
178 str1 = str1.Replace("'", "’").Replace("--", "—").Replace("=", "=").Replace("or", "o r").Replace("Or", "O r").Replace("OR", "O R").Replace("oR", "o R").Replace(">", "〉").Replace("<", "〈");
179 return str1;
180 }
181 #endregion
182
183 #region 防Sql注入攻击
184 /// <summary>
185 /// 防Sql注入攻击
186 /// </summary>
187 /// <param name="strQueryName"></param>
188 /// <returns></returns>
189 public static string getSQLString(string strQueryName)
190 {
191 if (strQueryName == null) return "";
192 strQueryName = strQueryName.Replace("'", "’").Replace("--", "—").Replace("=", "=").Replace("or", "o r").Replace("Or", "O r").Replace("OR", "O R").Replace("oR", "o R").Replace(">", "〉").Replace("<", "〈");
193 return strQueryName;
194 }
195 #endregion
196 }