If you simply want to truncate text to a certain number of words, because you do not want a word will be cut, the code below will do the trick:
1 2 3 4 5 6 7 8 |
function limit_words($string, $nb_word){ $words = explode(" ",$string); if(count($words) > $nb_word){ return implode(" ", array_splice($words, 0, $nb_word)) . ' ...'; }else{ return implode(" ", array_splice($words, 0, $nb_word)); } } |