john mossel
john mossel

Reputation: 2156

reading odt files in php

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

Answers (4)

Nishant Bhatt
Nishant Bhatt

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

Skrol29
Skrol29

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

Olivier Berger
Olivier Berger

Reputation: 21

http://pear.php.net/package/OpenDocument may be what you need. Haven't used it myself, though.

Upvotes: 0

Harmon Wood
Harmon Wood

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

Related Questions