Forum Moderators: coopster

Message Too Old, No Replies

Dear Techies,How can I count words in a word file using PHP?

Give me a idea

         

Vasanth

11:19 am on Mar 2, 2005 (gmt 0)

10+ Year Member



Dear Techies,How can I count words in a word file using PHP?The answer should match the wordcount of that particular word file

coopster

11:53 am on Mar 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Vasanth.

You could use file_get_contents() [php.net] to read the file into a string and then use str_word_count() [php.net].

charleyhorse

3:43 am on Mar 5, 2005 (gmt 0)



You could also use wc, a built in unix command (assuming you are running under *nix).

You'd probably want to call it thusly:

system("wc -w " . $filename);

wc can also count bytes and lines, if you call it with -c or -l, respectively.

I often use it to quickly analyze a log file ala

grep "oogle" mylog ¦ wc -l

to see how many times the string "oogle" appeared in the file "mylog".