Page 1 of 1

[SOLVED] Display only first 20 words from article.

Posted: Mon Jun 21, 2004 9:42 am
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

Posted: Mon Jun 21, 2004 9:52 am
by markl999

Code: Select all

$howmany = 20;
$words = join(' ', array_slice(explode(' ', $article), 0, $howmany));

Posted: Mon Jun 21, 2004 9:53 am
by feyd
something like:

Code: Select all

preg_match('#^(\b[^\b]+\b){20}#',$article,$matches);
echo $matches[0];

Posted: Mon Jun 21, 2004 9:56 am
by hairyjim
8O

whoosh...way over my head there guys.

I'll have a play.

Posted: Mon Jun 21, 2004 10:01 am
by feyd
mark's would be the simpler solution..

Posted: Mon Jun 21, 2004 10:33 am
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