[SOLVED] Display only first 20 words from article.

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
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

[SOLVED] Display only first 20 words from article.

Post by hairyjim »

Hi all,

Is there a string function that will allow me to only display lets say the first 20 words from an article?

I have had a look through the manual but since I am not familiar with most functions it is like looking for a needle in a haystack.

Cheers
Last edited by hairyjim on Mon Jun 21, 2004 10:32 am, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

$howmany = 20;
$words = join(' ', array_slice(explode(' ', $article), 0, $howmany));
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like:

Code: Select all

preg_match('#^(\b[^\b]+\b){20}#',$article,$matches);
echo $matches[0];
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

8O

whoosh...way over my head there guys.

I'll have a play.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mark's would be the simpler solution..
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Hi guys.

Yes Marks solution works a treat.

Thanks for this, I would never have worked this out, well perhaps I would but not so quick.

Jim
Post Reply