Hi,
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).
Thanks for the help!
function text_limit($text, $len, $end='...')
{
mb_internal_encoding('UTF-8');
if( (mb_strlen($text, 'UTF-8') > $len) ) {
$text = mb_substr($text, 0, $len, 'UTF-8');
$text = mb_substr($text, 0, mb_strrpos($text," ", 'UTF-8'), 'UTF-8');
...
}
}
Edit to add an example
If I truncate a text with 65 characters, it returns:
Un jardín de estilo neoclásico acorde con el …
If I change the special characters (í, á), then it returns:
Un jardin de estilo neoclasico acorde con el Palacio de …
I'm sure there is something strange with the encoding or the server, or php; but I can't figure it out! Thanks!
Final Solution
I'm using this UTF8 PHP library and everything works now...