title: Truncate text containing HTML, ignoring tags
body: <p>I want to truncate some text (loaded from a database or text file), but it contains HTML so as a result the tags are included and less text will be returned. This can then result in tags not being closed, or being partially closed (so Tidy may not work properly and there is still less content). How can I truncate based on the text (and probably stopping when you get to a table as that could cause more complex issues).</p>
  
  <pre><code>substr("Hello, my &lt;strong&gt;name&lt;/strong&gt; is &lt;em&gt;Sam&lt;/em&gt;. I&amp;acute;m a web developer.",0,26)."..."
  </code></pre>
  
  <p>Would result in:</p>
  
  <pre><code>Hello, my &lt;strong&gt;name&lt;/st...
  </code></pre>
  
  <p>What I would want is:</p>
  
  <pre><code>Hello, my &lt;strong&gt;name&lt;/strong&gt; is &lt;em&gt;Sam&lt;/em&gt;. I&amp;acute;m...
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p>While my question is for how to do it in PHP, it would be good to know how to do it in C#... either should be OK as I think I would be able to port the method over (unless it is a built in method).</p>
  
  <p>Also note that I have included an HTML entity <code>&amp;acute;</code> - which would have to be considered as a single character (rather than 7 characters as in this example).</p>
  
  <p><code>strip_tags</code> is a fallback, but I would lose formatting and links and it would still have the problem with HTML entities.</p>