| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- namespace System.Utility
- {
- public class StringHelper
- {
- #region HtmlEncode(对html字符串进行编码)
- /// <summary>
- /// 对html字符串进行编码
- /// </summary>
- /// <param name="html">html字符串</param>
- public static string HtmlEncode(string html)
- {
- return HttpUtility.HtmlEncode(html);
- }
- /// <summary>
- /// 对html字符串进行解码
- /// </summary>
- /// <param name="html">html字符串</param>
- public static string HtmlDecode(string html)
- {
- return HttpUtility.HtmlDecode(html);
- }
- #endregion
- #region UrlEncode(对Url进行编码)
- /// <summary>
- /// 对Url进行编码
- /// </summary>
- /// <param name="url">url</param>
- /// <param name="isUpper">编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"</param>
- public static string UrlEncode(string url, bool isUpper = false)
- {
- return UrlEncode(url, Encoding.UTF8, isUpper);
- }
- /// <summary>
- /// 对Url进行编码
- /// </summary>
- /// <param name="url">url</param>
- /// <param name="encoding">字符编码</param>
- /// <param name="isUpper">编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"</param>
- public static string UrlEncode(string url, Encoding encoding, bool isUpper = false)
- {
- var result = HttpUtility.UrlEncode(url, encoding);
- if (!isUpper)
- return result;
- return GetUpperEncode(result);
- }
- /// <summary>
- /// 获取大写编码字符串
- /// </summary>
- private static string GetUpperEncode(string encode)
- {
- var result = new StringBuilder();
- int index = int.MinValue;
- for (int i = 0; i < encode.Length; i++)
- {
- string character = encode[i].ToString();
- if (character == "%")
- index = i;
- if (i - index == 1 || i - index == 2)
- character = character.ToUpper();
- result.Append(character);
- }
- return result.ToString();
- }
- #endregion
- #region UrlDecode(对Url进行解码)
- /// <summary>
- /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
- /// </summary>
- /// <param name="url">url</param>
- public static string UrlDecode(string url)
- {
- return HttpUtility.UrlDecode(url);
- }
- /// <summary>
- /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
- /// </summary>
- /// <param name="url">url</param>
- /// <param name="encoding">字符编码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码</param>
- public static string UrlDecode(string url, Encoding encoding)
- {
- return HttpUtility.UrlDecode(url, encoding);
- }
- #endregion
- #region 去除HTML标记
- /// <summary>
- /// 去除HTML标记
- /// </summary>
- /// <param name="NoHTML">包括HTML的源码 </param>
- /// <returns>已经去除后的文字</returns>
- public static string NoHtml(string Htmlstring)
- {
- //删除脚本
- Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
- //删除HTML
- Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"…", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"—", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"“", "", RegexOptions.IgnoreCase);
- Htmlstring.Replace("<", "");
- Htmlstring = Regex.Replace(Htmlstring, @"”", "", RegexOptions.IgnoreCase);
- Htmlstring.Replace(">", "");
- Htmlstring.Replace("\r\n", "");
- Htmlstring = HttpUtility.HtmlEncode(Htmlstring).Trim();
- return Htmlstring;
- }
- #endregion
- #region 格式化文本(防止SQL注入)
- /// <summary>
- /// 格式化文本(防止SQL注入)
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string Formatstr(string html)
- {
- System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"select", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"update", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex(@"delete", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- html = regex1.Replace(html, ""); //过滤<script></script>标记
- html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
- html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
- html = regex4.Replace(html, ""); //过滤iframe
- html = regex10.Replace(html, "s_elect");
- html = regex11.Replace(html, "u_pudate");
- html = regex12.Replace(html, "d_elete");
- html = html.Replace("'", "’");
- html = html.Replace(" ", " ");
- return html;
- }
- #endregion
- /// <summary>
- /// SQL危险字符过滤
- /// </summary>
- /// <param name="source">要过滤的字符串</param>
- /// <param name="SafeLevel">安全级别{1最高}</param>
- /// <returns></returns>
- public static string FilterSql(string source, int SafeLevel)
- {
- //单引号替换成两个单引号
- source = source.Replace("'", "''");
- source = source.Replace("--", "--");
- if (SafeLevel == 1)
- {
- //单引号替换成两个单引号
- source = source.Replace("\"", "“");
- source = source.Replace("|", "|");
- //半角封号替换为全角封号,防止多语句执行
- source = source.Replace(";", ";");
- source = source.Replace("=", "=");
- //半角括号替换为全角括号
- source = source.Replace("(", "(");
- source = source.Replace(")", ")");
- }
- return source;
- }
- /// <summary>
- /// 检测是否有Sql危险字符
- /// </summary>
- /// <param name="str">要判断字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsSafeSqlString(string str)
- {
- return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
- }
- /// <summary>
- /// 检测是否有危险的可能用于链接的字符串
- /// </summary>
- /// <param name="str">要判断字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsSafeUserInfoString(string str)
- {
- return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
- }
- /// 转全角的函数(SBC case)
- ///
- ///任意字符串
- ///全角字符串
- ///
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- ///
- public static String ToSBC(String input)
- {
- // 半角转全角:
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 32)
- {
- c[i] = (char)12288;
- continue;
- }
- if (c[i] < 127)
- c[i] = (char)(c[i] + 65248);
- }
- return new String(c);
- }
- /// <summary>
- /// 转半角的函数(DBC case) 任意字符串
- /// 半角字符串 全角空格为12288,半角空格为32
- /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static String ToDBC(String input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- c[i] = (char)(c[i] - 65248);
- }
- return new String(c);
- }
- /// <summary>
- /// 去掉所有的非数字
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string RemoveNotNumber(string key)
- {
- return Regex.Replace(key, @"[^\d]*", "");
- }
- /// <summary>
- /// 去掉所有的数字
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string RemoveNumber(string key)
- {
- return Regex.Replace(key, @"\d", "");
- }
- }
- }
|