title: Cut an UTF8 text in PHP
body: <p>I get UTF8 text from a database, and I want to show only the first $len characters (finishing in a word). I've tried several options but the function still doesn't work because of special characters (á, é, í, ó, etc). </p>
  
  <p>Thanks for the help!</p>
  
  <pre><code>function text_limit($text, $len, $end='...')
  { 
  
    mb_internal_encoding('UTF-8');
    if( (mb_strlen($text, 'UTF-8') &gt; $len) ) { 
  
      $text = mb_substr($text, 0, $len, 'UTF-8');
      $text = mb_substr($text, 0, mb_strrpos($text," ", 'UTF-8'), 'UTF-8');
  
      ...
    }
  }
  </code></pre>
  
  <p><strong>Edit</strong> to add an example</p>
  
  <p>If I truncate a text with 65 characters, it returns:</p>
  
  <blockquote>
    <p>Un jardín de estilo neoclásico acorde con el …</p>
  </blockquote>
  
  <p>If I change the special characters (í, á), then it returns:</p>
  
  <blockquote>
    <p>Un jardin de estilo neoclasico acorde con el Palacio de …</p>
  </blockquote>
  
  <p>I'm sure there is something strange with the encoding or the server, or php; but I can't figure it out! Thanks!</p>
  
  <p><strong>Final Solution</strong></p>
  
  <p>I'm using this <a href="http://tarski.googlecode.com/svn/branches/1.6/library/feedparser/lib-utf8.php" rel="nofollow noreferrer">UTF8 PHP library</a> and everything works now...</p>