Article View: ba.php
Article #85Re: How to Count words?
From: chernyshevsky
Date: Mon, 14 Aug 2000 00:00
Date: Mon, 14 Aug 2000 00:00
52 lines
1879 bytes
1879 bytes
This should work:
function count_word($text)
{
$count = 0;
for($i = 0, $j = -1; $i < strlen($text); $i++)
{
switch($text[$i])
{
case "\r":
case "\n":
case "\t":
case " ":
if($i != $j + 1)
{
$count++;
}
$j = $i;
}
}
if($i != $j + 1)
{
$count++;
}
return $count;
}
Something like sizeof(explode(strtr(...))) doesn't work very well, since it
overcounts when you have consecutive delimiters (e.g. a space before a
linebreak). If you have to loop through the array anyway looking for empty
strings, might as well loop through the string instead.
Jeroen Straathof <jstraa@yahoo.com> wrote in message
news:8n6b5f$f6v$1@porthos.nl.uu.net...
> Hello,
>
> How do I count words in PHP ?
> I have version 3.0.17 installed.
>
> Regards,
>
> Jeroen
>
> --
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=
> Web: http://www.webtoolcentral.com
> Mail: jeroen@webperfex.nl
> ICQ: 22792936
> WebPager: http://wwp.mirabilis.com/22792936
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=
>
>
Technical Details
Message-ID:
<spf9mdhqn4t63@corp.supernews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8n6b5f$f6v$1@porthos.nl.uu.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 108904,96f5da7969c04587 X-Google-Attributes: gid108904,public X-Google-Thread: 10ce1c,96f5da7969c04587 X-Google-Attributes: gid10ce1c,public X-Google-Thread: 10b1d3,96f5da7969c04587 X-Google-Attributes: gid10b1d3,public From: <chernyshevsky@hotmail.com> Subject: Re: How to Count words? Date: 2000/08/14 Message-ID: <spf9mdhqn4t63@corp.supernews.com> X-Deja-AN: 658041378 References: <8n6b5f$f6v$1@porthos.nl.uu.net> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: newsabuse@supernews.com Organization: Posted via Supernews, http://www.supernews.com X-MSMail-Priority: Normal Newsgroups: ba.php,mailing.www.php-user,ufra.lists.php