足力健后端,使用.netcore版本,合并1个项目使用

StringHelper.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. namespace System.Utility
  9. {
  10. public class StringHelper
  11. {
  12. #region HtmlEncode(对html字符串进行编码)
  13. /// <summary>
  14. /// 对html字符串进行编码
  15. /// </summary>
  16. /// <param name="html">html字符串</param>
  17. public static string HtmlEncode(string html)
  18. {
  19. return HttpUtility.HtmlEncode(html);
  20. }
  21. /// <summary>
  22. /// 对html字符串进行解码
  23. /// </summary>
  24. /// <param name="html">html字符串</param>
  25. public static string HtmlDecode(string html)
  26. {
  27. return HttpUtility.HtmlDecode(html);
  28. }
  29. #endregion
  30. #region UrlEncode(对Url进行编码)
  31. /// <summary>
  32. /// 对Url进行编码
  33. /// </summary>
  34. /// <param name="url">url</param>
  35. /// <param name="isUpper">编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"</param>
  36. public static string UrlEncode(string url, bool isUpper = false)
  37. {
  38. return UrlEncode(url, Encoding.UTF8, isUpper);
  39. }
  40. /// <summary>
  41. /// 对Url进行编码
  42. /// </summary>
  43. /// <param name="url">url</param>
  44. /// <param name="encoding">字符编码</param>
  45. /// <param name="isUpper">编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"</param>
  46. public static string UrlEncode(string url, Encoding encoding, bool isUpper = false)
  47. {
  48. var result = HttpUtility.UrlEncode(url, encoding);
  49. if (!isUpper)
  50. return result;
  51. return GetUpperEncode(result);
  52. }
  53. /// <summary>
  54. /// 获取大写编码字符串
  55. /// </summary>
  56. private static string GetUpperEncode(string encode)
  57. {
  58. var result = new StringBuilder();
  59. int index = int.MinValue;
  60. for (int i = 0; i < encode.Length; i++)
  61. {
  62. string character = encode[i].ToString();
  63. if (character == "%")
  64. index = i;
  65. if (i - index == 1 || i - index == 2)
  66. character = character.ToUpper();
  67. result.Append(character);
  68. }
  69. return result.ToString();
  70. }
  71. #endregion
  72. #region UrlDecode(对Url进行解码)
  73. /// <summary>
  74. /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
  75. /// </summary>
  76. /// <param name="url">url</param>
  77. public static string UrlDecode(string url)
  78. {
  79. return HttpUtility.UrlDecode(url);
  80. }
  81. /// <summary>
  82. /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
  83. /// </summary>
  84. /// <param name="url">url</param>
  85. /// <param name="encoding">字符编码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码</param>
  86. public static string UrlDecode(string url, Encoding encoding)
  87. {
  88. return HttpUtility.UrlDecode(url, encoding);
  89. }
  90. #endregion
  91. #region 去除HTML标记
  92. /// <summary>
  93. /// 去除HTML标记
  94. /// </summary>
  95. /// <param name="NoHTML">包括HTML的源码 </param>
  96. /// <returns>已经去除后的文字</returns>
  97. public static string NoHtml(string Htmlstring)
  98. {
  99. //删除脚本
  100. Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
  101. //删除HTML
  102. Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
  103. Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
  104. Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  105. Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  106. Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  107. Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  108. Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
  109. Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
  110. Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
  111. Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  112. Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  113. Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  114. Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  115. Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
  116. Htmlstring = Regex.Replace(Htmlstring, @"&hellip;", "", RegexOptions.IgnoreCase);
  117. Htmlstring = Regex.Replace(Htmlstring, @"&mdash;", "", RegexOptions.IgnoreCase);
  118. Htmlstring = Regex.Replace(Htmlstring, @"&ldquo;", "", RegexOptions.IgnoreCase);
  119. Htmlstring.Replace("<", "");
  120. Htmlstring = Regex.Replace(Htmlstring, @"&rdquo;", "", RegexOptions.IgnoreCase);
  121. Htmlstring.Replace(">", "");
  122. Htmlstring.Replace("\r\n", "");
  123. Htmlstring = HttpUtility.HtmlEncode(Htmlstring).Trim();
  124. return Htmlstring;
  125. }
  126. #endregion
  127. #region 格式化文本(防止SQL注入)
  128. /// <summary>
  129. /// 格式化文本(防止SQL注入)
  130. /// </summary>
  131. /// <param name="str"></param>
  132. /// <returns></returns>
  133. public static string Formatstr(string html)
  134. {
  135. System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  136. System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  137. System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  138. System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  139. System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  140. System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"select", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  141. System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"update", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  142. System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex(@"delete", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  143. html = regex1.Replace(html, ""); //过滤<script></script>标记
  144. html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
  145. html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
  146. html = regex4.Replace(html, ""); //过滤iframe
  147. html = regex10.Replace(html, "s_elect");
  148. html = regex11.Replace(html, "u_pudate");
  149. html = regex12.Replace(html, "d_elete");
  150. html = html.Replace("'", "’");
  151. html = html.Replace("&nbsp;", " ");
  152. return html;
  153. }
  154. #endregion
  155. /// <summary>
  156. /// SQL危险字符过滤
  157. /// </summary>
  158. /// <param name="source">要过滤的字符串</param>
  159. /// <param name="SafeLevel">安全级别{1最高}</param>
  160. /// <returns></returns>
  161. public static string FilterSql(string source, int SafeLevel)
  162. {
  163. //单引号替换成两个单引号
  164. source = source.Replace("'", "''");
  165. source = source.Replace("--", "--");
  166. if (SafeLevel == 1)
  167. {
  168. //单引号替换成两个单引号
  169. source = source.Replace("\"", "“");
  170. source = source.Replace("|", "|");
  171. //半角封号替换为全角封号,防止多语句执行
  172. source = source.Replace(";", ";");
  173. source = source.Replace("=", "=");
  174. //半角括号替换为全角括号
  175. source = source.Replace("(", "(");
  176. source = source.Replace(")", ")");
  177. }
  178. return source;
  179. }
  180. /// <summary>
  181. /// 检测是否有Sql危险字符
  182. /// </summary>
  183. /// <param name="str">要判断字符串</param>
  184. /// <returns>判断结果</returns>
  185. public static bool IsSafeSqlString(string str)
  186. {
  187. return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
  188. }
  189. /// <summary>
  190. /// 检测是否有危险的可能用于链接的字符串
  191. /// </summary>
  192. /// <param name="str">要判断字符串</param>
  193. /// <returns>判断结果</returns>
  194. public static bool IsSafeUserInfoString(string str)
  195. {
  196. return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
  197. }
  198. /// 转全角的函数(SBC case)
  199. ///
  200. ///任意字符串
  201. ///全角字符串
  202. ///
  203. ///全角空格为12288,半角空格为32
  204. ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  205. ///
  206. public static String ToSBC(String input)
  207. {
  208. // 半角转全角:
  209. char[] c = input.ToCharArray();
  210. for (int i = 0; i < c.Length; i++)
  211. {
  212. if (c[i] == 32)
  213. {
  214. c[i] = (char)12288;
  215. continue;
  216. }
  217. if (c[i] < 127)
  218. c[i] = (char)(c[i] + 65248);
  219. }
  220. return new String(c);
  221. }
  222. /// <summary>
  223. /// 转半角的函数(DBC case) 任意字符串
  224. /// 半角字符串 全角空格为12288,半角空格为32
  225. /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  226. /// </summary>
  227. /// <param name="input"></param>
  228. /// <returns></returns>
  229. public static String ToDBC(String input)
  230. {
  231. char[] c = input.ToCharArray();
  232. for (int i = 0; i < c.Length; i++)
  233. {
  234. if (c[i] == 12288)
  235. {
  236. c[i] = (char)32;
  237. continue;
  238. }
  239. if (c[i] > 65280 && c[i] < 65375)
  240. c[i] = (char)(c[i] - 65248);
  241. }
  242. return new String(c);
  243. }
  244. /// <summary>
  245. /// 去掉所有的非数字
  246. /// </summary>
  247. /// <param name="key"></param>
  248. /// <returns></returns>
  249. public static string RemoveNotNumber(string key)
  250. {
  251. return Regex.Replace(key, @"[^\d]*", "");
  252. }
  253. /// <summary>
  254. /// 去掉所有的数字
  255. /// </summary>
  256. /// <param name="key"></param>
  257. /// <returns></returns>
  258. public static string RemoveNumber(string key)
  259. {
  260. return Regex.Replace(key, @"\d", "");
  261. }
  262. }
  263. }