BaseExtensions.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (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. }