Count number of words from (doc txt docx ) files
<p>I'm trying to count number of words in file. The following code is working fine with <code>.txt</code> file. But When I try to read <code>.doc docx .xls</code> files. Its give me wrong output. Please suggest me any third party plugin. Please help me . thanks</p>

<pre><code>$str = file_get_contents($path);

function count_words($string)
{
    $string = htmlspecialchars_decode(strip_tags($string));
    if (strlen($string)==0)
        return 0;
    $t = array(' '=&gt;1, '_'=&gt;1, "\x20"=&gt;1, "\xA0"=&gt;1, "\x0A"=&gt;1, "\x0D"=&gt;1, "\x09"=&gt;1, "\x0B"=&gt;1, "\x2E"=&gt;1, "\t"=&gt;1, '='=&gt;1, '+'=&gt;1, '-'=&gt;1, '*'=&gt;1, '/'=&gt;1, '\\'=&gt;1, ','=&gt;1, '.'=&gt;1, ';'=&gt;1, ':'=&gt;1, '"'=&gt;1, '\''=&gt;1, '['=&gt;1, ']'=&gt;1, '{'=&gt;1, '}'=&gt;1, '('=&gt;1, ')'=&gt;1, '&lt;'=&gt;1, '&gt;'=&gt;1, '&amp;'=&gt;1, '%'=&gt;1, '$'=&gt;1, '@'=&gt;1, '#'=&gt;1, '^'=&gt;1, '!'=&gt;1, '?'=&gt;1); // separators
    $count= isset($t[$string[0]])? 0:1;
    if (strlen($string)==1)
        return $count;
    for ($i=1;$i&lt;strlen($string);$i++)
        if (isset($t[$string[$i-1]]) &amp;&amp; !isset($t[$string[$i]])) // if new word starts
            $count++;
    return $count;
}
    echo count_words($str);
</code></pre>
