The problem with accepted answer is that result string goes over the the limit, i.e. it can exceed 100 chars since strpos will look after the offset and so your length will always be a over your limit. If the last word is long, like squirreled then the length of your result will be 111 (to give you an idea).
A better solution is to use wordwrap function:
function truncate($str, $length = 125, $append = '...') {
if (strlen($str) > $length) {
$delim = "~\n~";
$str = substr($str, 0, strpos(wordwrap($str, $length, $delim), $delim)) . $append;
}
return $str;
}
echo truncate("The quick brown fox jumped over the lazy dog.", 5);
This way you can be sure the string is truncated under your limit (and never goes over)
P.S. This is particularly useful if you plan to store the truncated string in your database with a fixed-with column like VARCHAR(50), etc.
P.P.S. Note the special delimiter in wordwrap. This is to make sure that your string is truncated correctly even when it contains newlines (otherwise it will truncate at first newline which you don't want).