I have two string that i want to limit to lets say the first 25 characters for example. Is there a way to cut off text after the 25th character and add a ... to the end of the s
This one is short and takes word boundary into account, it doesn't use loops which makes it very efficient
function truncate($str, $chars, $end = '...') {
if (strlen($str) <= $chars) return $str;
$new = substr($str, 0, $chars + 1);
return substr($new, 0, strrpos($new, ' ')) . $end;
}
Usage:
truncate('My string', 5); //returns: My...