题
您将如何阅读PHP中的ODT文件?我知道您可以使用Querypath,但这似乎有点过分,..我只想阅读该文件。
解决方案
ODT,文件为ZIP压缩XML。
如果您需要做的就是阅读文件。只需解压缩并像普通文件一样阅读。
如果您需要解析可用的文本,则输入需要Querypath或其他一些XSLT解析器。
其他提示
http://pear.php.net/package/opendocument 可能是您需要的。不过,我自己还没有使用过。
/*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);
不隶属于 StackOverflow