There are alot of ways to do this...but basically it goes like this...
- strip html tags out
- explode works into an array
- get the number of words you need
- output
This is an old thing I have laying around for doing this...but like I said, there are really alot of approaches you can consider for doing this...
function reduceWords($text,$num)
{
$text = strip_tags($text);
if (!$text) return NULL;
// reduce number of words to a constant amount
$text = explode(" ", $text);
$n = count($text);
if ($n <= $num) {
// outputs all text if it's less than your $num
$text = implode(" ", $text);
return $text;
} else {
// this is where it really reduces things...
$text = array_slice($text, 0, $num-5);
$text = implode(" ", $text);
return "$text...";
}
}
Call it up...
echo reduceWords($yourString,50);