How can I get the first n characters of a string in PHP? What\'s the fastest way to trim a string to a specific number of characters, and append \'...\' if needed?
The Multibyte extension can come in handy if you need control over the string charset.
$charset = 'UTF-8'; $length = 10; $string = 'Hai to yoo! I like yoo soo!'; if(mb_strlen($string, $charset) > $length) { $string = mb_substr($string, 0, $length - 3, $charset) . '...'; }