title: Substring without breaking html c#
body: <p>Hi guys I'm trying to take a description which has been entered in a wysiwyg editor and take a substring of it..</p>
  
  <p>i.e</p>
  
  <pre><code>This is some &lt;span style="font-weight:bold;"&gt;text&lt;/span&gt;
  </code></pre>
  
  <p>I'd like to limit some descriptions without breaking the html if i just substring and add ...</p>
  
  <p>it breaks the html tags.. </p>
  
  <p>I've tried:</p>
  
  <pre><code>string HtmlSubstring(string html, int maxlength)
      {
          string htmltag = "&lt;/?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\"&gt;\\s]+))?)+\\s*|\\s*)/?&gt;";
          string emptytags = "&lt;(\\w+)((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\"&gt;\\s]+))?)+\\s*|\\s*)/?&gt;&lt;/\\1&gt;";
  
          var expression = new Regex(string.Format("({0})|(.?)", htmltag));
          MatchCollection matches = expression.Matches(html);
          int i = 0;
  
          StringBuilder content = new StringBuilder();
          foreach (Match match in matches)
          {
              if (match.Value.Length == 1 &amp;&amp; i &lt; maxlength)
              {
                  content.Append(match.Value);
                  i++;
              }
              else if (match.Value.Length &gt; 1)
              {
                  content.Append(match.Value);
              }
          }
          return Regex.Replace(content.ToString(), emptytags, string.Empty);
      }
  </code></pre>
  
  <p>but it doesn't quite get me there!</p>