Files
StoreWeb/Store.KJDWebService/Commons/Helper/HtmlHelper.cs
T

216 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NaiBa.Util.Extension;
namespace NaiBa.Util.Helper
{
public class HtmlHelper
{
public static string HtmlEncode(string html)
{
if (string.IsNullOrEmpty(html))
{
return string.Empty;
}
else
{
return WebUtility.HtmlEncode(html);
}
}
public static string HtmlDecode(string url)
{
if (string.IsNullOrEmpty(url))
{
return string.Empty;
}
else
{
return WebUtility.HtmlDecode(url);
}
}
/// <summary>
/// Unicode解码
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string UnicodeDecode(string html)
{
html = WebUtility.HtmlDecode(html);
Regex reUnicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled);
return reUnicode.Replace(html, m =>
{
short c;
if (short.TryParse(m.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c))
{
return "" + (char)c;
}
return m.Value;
});
}
#region Resove
public static string Resove(string html, string prefix, string subfix = "")
{
int inl = html.IndexOf(prefix);
if (inl == -1)
{
prefix = prefix.Replace("\"", "'");
subfix = subfix.Replace("\"", "'");
inl = html.IndexOf(prefix);
}
if (inl == -1)
{
return null;
}
inl += prefix.Length;
if (!string.IsNullOrEmpty(subfix))
{
int inl2 = html.IndexOf(subfix, inl);
if (inl2 == -1)
{
return null;
}
string result = html.Substring(inl, inl2 - inl);
return result;
}
else
{
string result = html.Substring(inl);
return result;
}
}
#endregion
#region ResoveReverse
public static string ResoveReverse(string html, string subfix, string prefix)
{
int inl = html.IndexOf(subfix);
if (inl == -1)
{
return null;
}
string subString = html.Substring(0, inl);
int inl2 = subString.LastIndexOf(prefix);
if (inl2 == -1)
{
return null;
}
string s = subString.Substring(inl2 + prefix.Length, subString.Length - inl2 - prefix.Length);
return s;
}
#endregion
#region ResoveList
public static List<string> ResoveList(string html, string prefix, string subfix)
{
List<string> list = new List<string>();
int index = 0;
do
{
index = html.IndexOf(prefix, index);
if (index == -1)
{
break;
}
index += prefix.Length;
int indexEnd = html.IndexOf(subfix, index);
if (indexEnd > 0)
{
string content = html.Substring(index, indexEnd - index);
list.Add(content);
}
index = indexEnd;
}
while (index > -1);
return list;
}
#endregion
#region ResoveListByAT
public static List<string> ResoveListByAT(string html)
{
List<string> list = new List<string>();
int index = html.IndexOf('@');
while (index > 0)
{
int indexx = html.LastIndexOf("'", index); //向前一个单引号
int indexy = html.IndexOf("'", index + 1); //向后一个单引号
string s78 = html.Substring(indexx, indexy - indexx);
s78 = s78.Replace("'", "");
list.Add(s78);
index = html.IndexOf('@', indexy);
}
return list;
}
#endregion
/// <summary>
/// 清除HTML代码
/// </summary>
/// <param name="html"></param>
public static string ClearHtml(string html)
{
html = html.ParseToString();
html = Regex.Replace(html, @"(<head[\w\W]*?</head>|<script[\w\W]*?</script>|<style[\w\W]*?</style>|<!--[\w\W]*?-->)", string.Empty);
html = Regex.Replace(html, @"(<span[\w\W]*?</span>)", string.Empty);
html = Regex.Replace(html, "(<[^>]*?>)", string.Empty);
return html;
}
public static string GetHtmlText(string html)
{
html = html.ParseToString();
html = Regex.Replace(html, "<.*?>", string.Empty);
return html;
}
/// <summary>
/// 清除HTML代码A标签
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string ClearHtmlA(string html)
{
html = html.ParseToString();
html = Regex.Replace(html, "(</?a[^>]*>)", "", RegexOptions.IgnoreCase);
return html.Trim();
}
public static string AddEscapeCharacter(string html)
{
html = html.ParseToString();
html = html.Replace("\"", "\\\"");
html = html.Replace("'", "\\'");
return html;
}
public static string TrimBOM(string html)
{
html = html.Trim(new char[] { '\uFEFF' });
return html;
}
public static string TrimJsonp(string json)
{
int start = json.IndexOf("{");
int end = json.LastIndexOf("}");
if (start > 0 && end > 0)
{
return json.Substring(start, end - start + 1);
}
return json;
}
}
}