title: How to truncate an UTF8 string in PHP?
body: <p>I have some strings in my PHP code that need to be truncated if they are too long.</p>
  
  <p>For example if a text is something like this:</p>
  
  <blockquote>
    <p>Hi, I would like to tell you how wonderful this is.</p>
  </blockquote>
  
  <p>It would replace it with this:</p>
  
  <blockquote>
    <p>Hi, I would like to ...</p>
  </blockquote>
  
  <p>For that I've done a simple <code>substr</code>. The problem is that in UTF8 some characters are actually two characters long. And I've had some problems with a character being cut in the middle: For example, when I try to insert the modified string in the database, it crashes.</p>
  
  <p>Here is my current function:</p>
  
  <pre><code>static function short($string, $max = 255){
     if(strlen($string) &gt;= $max){
         $string = substr($string, 0, $max - 5).'...';
     } return $string;
  }
  </code></pre>
  
  <p>Would someone know a way to make this function work even for UTF8 characters?</p>