Reputation: 117
I am able to extract the text content of a Docx File, I want to do the same for Doc file. I tried using the same code but could not read anything. I guess the reason is "Doc formats are not zipped archives." Here is the code:
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 "";
}
Please let me know if there is a way to fetch text content from Doc file.
Upvotes: 0
Views: 5094
Reputation: 117
Well I finally got the Answer, so thought I should share it here. I simply used COM Objects:
$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);
Upvotes: 4