BaseExtensions.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace MediaBrowser.Common.Extensions
  6. {
  7. /// <summary>
  8. /// Class BaseExtensions
  9. /// </summary>
  10. public static class BaseExtensions
  11. {
  12. /// <summary>
  13. /// Strips the HTML.
  14. /// </summary>
  15. /// <param name="htmlString">The HTML string.</param>
  16. /// <returns>System.String.</returns>
  17. public static string StripHtml(this string htmlString)
  18. {
  19. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  20. const string pattern = @"<(.|\n)*?>";
  21. return Regex.Replace(htmlString, pattern, string.Empty).Trim();
  22. }
  23. /// <summary>
  24. /// Gets the M d5.
  25. /// </summary>
  26. /// <param name="str">The STR.</param>
  27. /// <returns>Guid.</returns>
  28. public static Guid GetMD5(this string str)
  29. {
  30. using (var provider = MD5.Create())
  31. {
  32. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  33. }
  34. }
  35. /// <summary>
  36. /// Gets the MB id.
  37. /// </summary>
  38. /// <param name="str">The STR.</param>
  39. /// <param name="aType">A type.</param>
  40. /// <returns>Guid.</returns>
  41. /// <exception cref="System.ArgumentNullException">aType</exception>
  42. public static Guid GetMBId(this string str, Type aType)
  43. {
  44. if (aType == null)
  45. {
  46. throw new ArgumentNullException("aType");
  47. }
  48. return (aType.FullName + str.ToLower()).GetMD5();
  49. }
  50. /// <summary>
  51. /// Gets the attribute value.
  52. /// </summary>
  53. /// <param name="str">The STR.</param>
  54. /// <param name="attrib">The attrib.</param>
  55. /// <returns>System.String.</returns>
  56. /// <exception cref="System.ArgumentNullException">attrib</exception>
  57. public static string GetAttributeValue(this string str, string attrib)
  58. {
  59. if (attrib == null)
  60. {
  61. throw new ArgumentNullException("attrib");
  62. }
  63. string srch = "[" + attrib + "=";
  64. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  65. if (start > -1)
  66. {
  67. start += srch.Length;
  68. int end = str.IndexOf(']', start);
  69. return str.Substring(start, end - start);
  70. }
  71. return null;
  72. }
  73. }
  74. }