Count number of words from (doc txt docx ) files

前端 未结 2 1682
离开以前
离开以前 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('', " ", $content);
        $content = str_replace('', "\r\n", $content);
        $striped_content = strip_tags($content);
    
        return $striped_content;
    }
    

提交回复
热议问题