How can I truncate a string to the first 20 words in PHP? ( word wrap )

Truncate strings by words 
PHP Function : 
function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
= str_word_count($text, 2);
= array_keys($words);
= substr($text, 0, $pos[$limit]) . '...';
return $text;

echo limit_text
('Hello here is a long sentence blah blah blah blah blah hahahaha haha haaaaaa', 5);



Hello here is a long ...



Developer, Technology aficionado, Beer Lover and founder of Network. Who shares a keen interest about website development, health issues, cancer awareness and other fun stuff.

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most popular

Most discussed