Helping ordinary people create extraordinary websites!
   

How to limit the number of words in my display with PHP?

Thursday, 27th August 2009
by Stevo


I would like to show only the first 10 words of a string. What is the php code to limit the number of words I want to show? Thanks.



Texas
Add the below function to your site

function string_limit_words($string, $word_limit) {
$words = explode(' ', $string);
return implode(' ', array_slice($words, 0, $word_limit));
}

Then you can set the values for the string and the word limit whenever you need to.

Eg.
echo string_limit_words($paragraph, 15);

So in the example above only the first fifteen words of the string $paragraph will be displayed.
Friday, 28th August 2009
Votes:
30
19

More PHP Help:
» How can i build a web page that have names of people
» Best ways to learn to develop online software using PHP and MySQL?
» How to create a Simple OAuth Application in PHP?
» How can I set extension on run time?
» How to configure php on my system?
» Which PHP ecommerce shopping cart software should I purchase?
» How to upload .doc file using php?
» PHP get current page's URL