title: php trim a string
body: <p>I'm trying to build a function to trim a string is it's too long per my specifications.</p>
  
  <p>Here's what I have:</p>
  
  <pre><code>function trim_me($s,$max) 
  {
      if (strlen($s) &gt; $max) 
      {
          $s = substr($s, 0, $max - 3) . '...';
      }
      return $s;
  }
  </code></pre>
  
  <p>The above will trim a string if it's longer than the <code>$max</code> and will add a continuation<code>...</code></p>
  
  <p>I want to expand that function to handle multiple words. Currently it does what it does, but if I have a string say: <code>How are you today?</code> which is 18 characters long. If I run <code>trim_me($s,10)</code> it will show as <code>How are yo...</code>, which is not aesthetically pleasing. How can I make it so it adds a <code>...</code> after the whole word. Say if I run <code>trim_me($s,10)</code> I want it to display <code>How are you...</code> adding the continuation AFTER the word. Any ideas?</p>
  
  <p>I pretty much don't want to add a continuation in the middle of a word. But if the string has only one word, then the continuation can break the word then only.</p>