Muhamad Kwexa
Muhamad Kwexa

Reputation: 33

how to limit number of words in a description in php?

I want to display a small intro of with a limited number of words without cutting the last word if it is too long... I know there is a php function substr() but it does not display the intro like "this is my starting text..." It makes it say "this is my starting t..." in PHP. Thank you for any answer.

                    <a href="#">'.substr($rows['title'],0,50).'</a>

Upvotes: 2

Views: 4015

Answers (1)

Thanatos11th
Thanatos11th

Reputation: 198

why not this:

implode(' ', array_slice(explode(' ', $rows['title']), 0, 50)); 
  1. explode all the words;
  2. get first 50;
  3. recreate the sentence containing only first 50 words;

Upvotes: 5

Related Questions