BaseExtensions.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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="type">The type.</param>
  40. /// <returns>Guid.</returns>
  41. public static Guid GetMBId(this string str, Type type)
  42. {
  43. if (type == null)
  44. {
  45. throw new ArgumentNullException("type");
  46. }
  47. var key = type.FullName + str.ToLower();
  48. return key.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 (string.IsNullOrEmpty(str))
  60. {
  61. throw new ArgumentNullException("str");
  62. }
  63. if (string.IsNullOrEmpty(attrib))
  64. {
  65. throw new ArgumentNullException("attrib");
  66. }
  67. string srch = "[" + attrib + "=";
  68. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  69. if (start > -1)
  70. {
  71. start += srch.Length;
  72. int end = str.IndexOf(']', start);
  73. return str.Substring(start, end - start);
  74. }
  75. return null;
  76. }
  77. }
  78. }