I wipped up this little script after there seems to be some interest in ending a string after so many WORDS instead of letters, this script is alittle larger than the oneposted above but it works, ive tested it.
<?php
// String we will be using to break up into literal words
$string = "Mommie, wow! I'm a big kid now!";
// Maximum amount of words
$max = 5;
// lets trim the beggining and end of the string to make sure
// there are no spaces that will create 'empty' words in the
// next step
$string = trim($string);
// lets make words out of this string!
$word = explode(" ", $string);
//now to count how many wods we have (7)
$total_words = count($word);
// now to see if there are to many, and if there are display
// only up to the maximum allowed
if($total_words > $max){
for($i = 0; $i <= $max; $i++){
if($i == $max){
echo $word[$max]."....";
// this is the cut off point, the rest
// of the string is replaced with
// trailing periods "...."
}else{
echo $word[$i]." ";
// as long as the variable $i is not
// equal to the maximum words allowed
// each word will trail with a space
// up until th max limit is reached
}
}
}else{
echo $string;
// the string does not contain more words than the
// maximum allowed, so display the entire string
}
?>