# Count the number of words in a document using PHP

**URL:** <https://forum.kirupa.com/t/count-the-number-of-words-in-a-document-using-php/253170>\
**Category:** programming\
**Created:** [February 27, 2008, 9:14pm UTC](https://forum.kirupa.com/t/count-the-number-of-words-in-a-document-using-php/253170 "2008-02-27T21:14:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andres](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@andres](https://forum.kirupa.com/u/andres)\
**Post date:** [February 27, 2008, 9:14pm UTC](https://forum.kirupa.com/t/count-the-number-of-words-in-a-document-using-php/253170/1 "2008-02-27T21:14:48Z")

</div>

Im trying to count the number of words in a document using PHP.  
I already have a function that returns the number of words in a string:

```php

function wordCount($string){   
     $words = "";   
     $string = eregi_replace(" +", " ", $string);   
     $array = explode(" ", $string);   
     for($i=0;$i < count($array);$i++) {   
         if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i])) $words[$i] = $array[$i];   
  }   
     return $words;   
 }  

```

So I thought I could try to read the file and put the content in a string but its not working.

BTW, the documents Im trying to do this with, are Word, Excel and PDF…

Any ideas on how to do this???

Thanks.

---

<div class="post-metadata">

**Author:** ![jmam](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@jmam](https://forum.kirupa.com/u/jmam)\
**Post date:** [July 4, 2008, 2:09pm UTC](https://forum.kirupa.com/t/count-the-number-of-words-in-a-document-using-php/253170/2 "2008-07-04T14:09:05Z")

</div>

Andres:

Did you find a solution for this counting problem? Would you give me a hint please? I am also having the same challenge now.

Thanks,

jmam.

---

<div class="post-metadata">

**Author:** ![Templarian](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/templarian/32/7152_2.png) [@Templarian](https://forum.kirupa.com/u/Templarian)\
**Post date:** [July 4, 2008, 2:22pm UTC](https://forum.kirupa.com/t/count-the-number-of-words-in-a-document-using-php/253170/3 "2008-07-04T14:22:52Z")

</div>

jmam the word document is just a zip archive. Simply change the docx to a zip and open it and find which file holds the content (I forget which one).

After that use the zip api to open it into memory and read the xml document and grab the nodes content.

Hope that helps you out.
