Can you restrict number of words show?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you restrict number of words show?

Post by simonmlewis »

Code: Select all

$boneyard=100; //Defines how many characters will be displayed from content field.
$postyard = substr($row->description,0,$boneyard);
echo "$postyard <font color='#cccccc'>...</font>";
We are trying to show a set number of words from the description to show on the category page, but doing it as Characters isn't working as you get half the word.

So, is there a way to do this, but for WORDS not characters?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can you restrict number of words show?

Post by Celauran »

substr down to 100 chars, then print up to strrpos of space character? Maybe throw a trim on the end to remove punctuation as needed?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you restrict number of words show?

Post by requinix »

str_word_count can return an array of the words in a string and array_slice can get you a portion of an array...
Post Reply