2013年12月29日 星期日

HTML Removal 類別庫(移除Html標籤)

HTML Removal 類別庫(移除Html標籤)

  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Web;
  8.  
  9.  
  10.  
  11. /// <summary>
  12.  
  13. /// HtmlRemoval 的摘要描述
  14.  
  15. /// </summary>
  16.  
  17. using System;
  18.  
  19. using System.Text.RegularExpressions;
  20.  
  21.  
  22.  
  23. /// <summary>
  24.  
  25. /// Methods to remove HTML from strings.
  26.  
  27. /// </summary>
  28.  
  29. public static class HtmlRemoval
  30.  
  31. {
  32.  
  33.     /// <summary>
  34.  
  35.     /// Remove HTML from string with Regex.
  36.  
  37.     /// </summary>
  38.  
  39.     public static string StripTagsRegex(string source)
  40.  
  41.     {
  42.  
  43.         return Regex.Replace(source, "<.*?>", string.Empty);
  44.  
  45.     }
  46.  
  47.  
  48.  
  49.     /// <summary>
  50.  
  51.     /// Compiled regular expression for performance.
  52.  
  53.     /// </summary>
  54.  
  55.     static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
  56.  
  57.  
  58.  
  59.     /// <summary>
  60.  
  61.     /// Remove HTML from string with compiled Regex.
  62.  
  63.     /// </summary>
  64.  
  65.     public static string StripTagsRegexCompiled(string source)
  66.  
  67.     {
  68.  
  69.         return _htmlRegex.Replace(source, string.Empty);
  70.  
  71.     }
  72.  
  73.  
  74.  
  75.     /// <summary>
  76.  
  77.     /// Remove HTML tags from string using char array.
  78.  
  79.     /// </summary>
  80.  
  81.     public static string StripTagsCharArray(string source)
  82.  
  83.     {
  84.  
  85.         char[] array = new char[source.Length];
  86.  
  87.         int arrayIndex = 0;
  88.  
  89.         bool inside = false;
  90.  
  91.  
  92.  
  93.         for (int i = 0; i < source.Length; i++)
  94.  
  95.         {
  96.  
  97.             char let = source[i];
  98.  
  99.             if (let == '<')
  100.  
  101.             {
  102.  
  103.                 inside = true;
  104.  
  105.                 continue;
  106.  
  107.             }
  108.  
  109.             if (let == '>')
  110.  
  111.             {
  112.  
  113.                 inside = false;
  114.  
  115.                 continue;
  116.  
  117.             }
  118.  
  119.             if (!inside)
  120.  
  121.             {
  122.  
  123.                 array[arrayIndex] = let;
  124.  
  125.                 arrayIndex++;
  126.  
  127.             }
  128.  
  129.         }
  130.  
  131.         return new string(array, 0, arrayIndex);
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     /// <summary>
  138.  
  139.     /// 尋找及取代 Visual C#.NET 中的 XML 檔案中的特殊字元
  140.  
  141.     /// </summary>
  142.  
  143.     /// <param name="source"></param>
  144.  
  145.     /// <returns></returns>
  146.  
  147.     public static string StripTagsRestore(string source)
  148.  
  149.     {
  150.  
  151.         string Rsource = source;
  152.  
  153.         try
  154.  
  155.         {
  156.  
  157.             Rsource = Rsource.Replace("&amp;", "&");
  158.  
  159.             Rsource = Rsource.Replace("&lt;", "<");
  160.  
  161.             Rsource = Rsource.Replace("&gt;", ">");
  162.  
  163.             Rsource = Rsource.Replace("&quto;", "\"");
  164.  
  165.             Rsource = Rsource.Replace("&apos;", "'");
  166.  
  167.         }
  168.  
  169.         catch (System.Exception)
  170.  
  171.         {
  172.  
  173.             //throw;
  174.  
  175.         }
  176.  
  177.         return Rsource;
  178.  
  179.     }
  180.  
  181. }
  182.  

參考來源:不知名網友(年代久遠已追溯不到來源)