Try this function:
function shorten_string($string, $wordsreturned)
{
$retval = $string;
$string = preg_replace('/(?<=\S,)(?=\S)/', ' ', $string);
$string = str_replace("\n", " ", $string);
$array = explode(" ", $string);
if (count($array)<=$wordsreturned)
{
$retval = $string;
}
else
{
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
}
return $retval;
}
On your text, so like this:
$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$firsttenwords = shorten_string($string, 10);
From here.
UPDATE: Now it's space-compliant, and also new-line compliant.