Count number of words from (doc txt docx ) files

前端 未结 2 1674
离开以前
离开以前 2021-01-26 10:12

I\'m trying to count number of words in file. The following code is working fine with .txt file. But When I try to read .doc docx .xls files. Its give

相关标签:
2条回答
  • 2021-01-26 10:53

    I am working in the same issues with you. All you need to do is parse the .doc docx .xls file in the right way. Then use the count_words

    private function read_docx(){
    
        $striped_content = '';
        $content = '';
    
        $zip = zip_open($this->filename);
    
        if (!$zip || is_numeric($zip)) return false;
    
        while ($zip_entry = zip_read($zip)) {
    
            if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
    
            if (zip_entry_name($zip_entry) != "word/document.xml") continue;
    
            $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
    
            zip_entry_close($zip_entry);
        }// end while
    
        zip_close($zip);
    
        $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
        $content = str_replace('</w:r></w:p>', "\r\n", $content);
        $striped_content = strip_tags($content);
    
        return $striped_content;
    }
    
    0 讨论(0)
  • 2021-01-26 11:01

    if you run on linux try this :

    system("wc -w " . $filename); 
    
    0 讨论(0)
提交回复
热议问题