Reputation: 2156
How would you go about reading odt files in PHP? I know you can use QueryPath, but that seems a bit of an overkill,.. I just want to read the file.
Upvotes: 6
Views: 9034
Reputation: 589
/*Name of the document file*/
$document = 'Template.odt';
/**Function to extract text*/
function extracttext($filename) {
$dataFile = "content.xml";
//Create a new ZIP archive object
$zip = new ZipArchive;
// Open the archive file
if (true === $zip->open($filename)) {
// If successful, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false) {
// Index found! Now read it to a string
$text = $zip->getFromIndex($index);
// Load XML from a string
// Ignore errors and warnings
$xml = new DOMDocument;
$xml->loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
// Return XML
return $xml->saveXML();
}
//Close the archive file
$zip->close();
}
// In case of failure return a message
return "File no`enter code here`t found";
}
echo extracttext($document);
Upvotes: 1
Reputation: 5597
OpenTBS is able to read and modify OpenDocument files in PHP.
Since OpenDocument files are XML files stored into a zip archive, you can also use the TbsZip class to simply read a zip archive under PHP without any other library dependency.
Upvotes: 2
Reputation: 21
http://pear.php.net/package/OpenDocument may be what you need. Haven't used it myself, though.
Upvotes: 0
Reputation: 2987
odt, files are zip compressed xml.
If all you need to do is read the file raw. Just unzip it and read it like a normal file.
If you need to parse out usable text, then enters the need for QueryPath or some other xslt parser.
Upvotes: 5