有没有办法在类似于 Docx 的 PHP 中读取 Doc 文件


Is there a way to read Doc files in PHP similar to Docx?

我能够提取Docx文件的文本内容,我想对Doc文件做同样的事情。我尝试使用相同的代码,但无法读取任何内容。我想原因是"文档格式不是压缩存档"。这是代码:

  function readDocx ($filePath) 
    {

        // Create new ZIP archive
        $zip = new ZipArchive;
        $dataFile = 'word/document.xml';
        // Open received archive file
        if (true === $zip->open($filePath)) {
            // If done, search for the data file in the archive
            if (($index = $zip->locateName($dataFile)) !== false) {
                // If found, read it to the string
                $data = $zip->getFromIndex($index);
                // Close archive file
                $zip->close();
                // Load XML from a string
                // Skip errors and warnings
                $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
                $contents = explode(''n',strip_tags($xml->saveXML()));
                $text = '';
                foreach($contents as $i=>$content) {
                    $text .= $contents[$i];
                }
                return $text;
            }
            $zip->close();
        }
        return "";
    }

请让我知道是否有办法从Doc文件中获取文本内容。

好吧,我终于得到了答案,所以我想我应该在这里分享它。我只是使用了 COM 对象:

$DocumentPath="C:/xampp/htdocs/abcd.doc";
$word = new COM("word.application") or die("Unable to instantiate application object");
$wordDocument = new COM("word.document") or die("Unable to instantiate document object");
$word->Visible = 0;
$wordDocument = $word->Documents->Open($DocumentPath);
$HTMLPath = substr_replace($DocumentPath, 'html', -3, 3);
$wordDocument->SaveAs($HTMLPath, 3);
$wordDocument = null;
$word->Quit();
$word = null;
readfile($HTMLPath);
unlink($HTMLPath);