Advertisement
Guest User

Untitled

a guest
Sep 7th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.08 KB | None | 0 0
  1. private string StripHTML(string source)
  2.             {
  3.                 try
  4.                 {
  5.                     string result;
  6.  
  7.                     // Remove HTML Development formatting
  8.                     // Replace line breaks with space
  9.                     // because browsers inserts space
  10.                     result = source.Replace("\r", " ");
  11.                     // Replace line breaks with space
  12.                     // because browsers inserts space
  13.                     result = result.Replace("\n", " ");
  14.                     // Remove step-formatting
  15.                     result = result.Replace("\t", string.Empty);
  16.                     // Remove repeating spaces because browsers ignore them
  17.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  18.                                                                             @"( )+", " ");
  19.  
  20.                     // Remove the header (prepare first by clearing attributes)
  21.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  22.                                 @"<( )*head([^>])*>","<head>",
  23.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  24.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  25.                                 @"(<( )*(/)( )*head( )*>)","</head>",
  26.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  27.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  28.                                 "(<head>).*(</head>)",string.Empty,
  29.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  30.  
  31.                     // remove all scripts (prepare first by clearing attributes)
  32.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  33.                                 @"<( )*script([^>])*>","<script>",
  34.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  35.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  36.                                 @"(<( )*(/)( )*script( )*>)","</script>",
  37.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  38.                     //result = System.Text.RegularExpressions.Regex.Replace(result,
  39.                     //         @"(<script>)([^(<script>\.</script>)])*(</script>)",
  40.                     //         string.Empty,
  41.                     //         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  42.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  43.                                 @"(<script>).*(</script>)",string.Empty,
  44.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  45.  
  46.                     // remove all styles (prepare first by clearing attributes)
  47.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  48.                                 @"<( )*style([^>])*>","<style>",
  49.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  50.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  51.                                 @"(<( )*(/)( )*style( )*>)","</style>",
  52.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  53.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  54.                                 "(<style>).*(</style>)",string.Empty,
  55.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  56.  
  57.                     // insert tabs in spaces of <td> tags
  58.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  59.                                 @"<( )*td([^>])*>","\t",
  60.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  61.  
  62.                     // insert line breaks in places of <BR> and <LI> tags
  63.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  64.                                 @"<( )*br( )*>","\r",
  65.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  66.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  67.                                 @"<( )*li( )*>","\r",
  68.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  69.  
  70.                     // insert line paragraphs (double line breaks) in place
  71.                     // if <P>, <DIV> and <TR> tags
  72.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  73.                                 @"<( )*div([^>])*>","\r\r",
  74.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  75.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  76.                                 @"<( )*tr([^>])*>","\r\r",
  77.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  78.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  79.                                 @"<( )*p([^>])*>","\r\r",
  80.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  81.  
  82.                     // Remove remaining tags like <a>, links, images,
  83.                     // comments etc - anything that's enclosed inside < >
  84.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  85.                                 @"<[^>]*>",string.Empty,
  86.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  87.  
  88.                     // replace special characters:
  89.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  90.                                 @" "," ",
  91.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  92.  
  93.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  94.                                 @"&bull;"," * ",
  95.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  96.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  97.                                 @"&lsaquo;","<",
  98.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  99.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  100.                                 @"&rsaquo;",">",
  101.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  102.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  103.                                 @"&trade;","(tm)",
  104.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  105.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  106.                                 @"&frasl;","/",
  107.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  108.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  109.                                 @"&lt;","<",
  110.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  111.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  112.                                 @"&gt;",">",
  113.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  114.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  115.                                 @"&copy;","(c)",
  116.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  117.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  118.                                 @"&reg;","(r)",
  119.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  120.                     // Remove all others. More can be added, see
  121.                     // http://hotwired.lycos.com/webmonkey/reference/special_characters/
  122.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  123.                                 @"&(.{2,6});", string.Empty,
  124.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  125.  
  126.                     // for testing
  127.                     //System.Text.RegularExpressions.Regex.Replace(result,
  128.                     //       this.txtRegex.Text,string.Empty,
  129.                     //       System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  130.  
  131.                     // make line breaking consistent
  132.                     result = result.Replace("\n", "\r");
  133.  
  134.                     // Remove extra line breaks and tabs:
  135.                     // replace over 2 breaks with 2 and over 4 tabs with 4.
  136.                     // Prepare first to remove any whitespaces in between
  137.                     // the escaped characters and remove redundant tabs in between line breaks
  138.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  139.                                 "(\r)( )+(\r)","\r\r",
  140.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  141.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  142.                                 "(\t)( )+(\t)","\t\t",
  143.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  144.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  145.                                 "(\t)( )+(\r)","\t\r",
  146.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  147.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  148.                                 "(\r)( )+(\t)","\r\t",
  149.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  150.                     // Remove redundant tabs
  151.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  152.                                 "(\r)(\t)+(\r)","\r\r",
  153.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  154.                     // Remove multiple tabs following a line break with just one tab
  155.                     result = System.Text.RegularExpressions.Regex.Replace(result,
  156.                                 "(\r)(\t)+","\r\t",
  157.                                 System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  158.                     // Initial replacement target string for line breaks
  159.                     string breaks = "\r\r\r";
  160.                     // Initial replacement target string for tabs
  161.                     string tabs = "\t\t\t\t\t";
  162.                     for (int index=0; index<result.Length; index++)
  163.                     {
  164.                         result = result.Replace(breaks, "\r\r");
  165.                         result = result.Replace(tabs, "\t\t\t\t");
  166.                         breaks = breaks + "\r";
  167.                         tabs = tabs + "\t";
  168.                     }
  169.  
  170.                     // That's it.
  171.                     return result;
  172.                 }
  173.                 catch
  174.                 {
  175.                     MessageBox.Show("Error");
  176.                     return source;
  177.                 }
  178.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement