Last updated: August 28, 2026.
A DOCX file is a ZIP package containing XML. For indexing or previews, read word/document.xml and combine its text nodes. This does not preserve layout.
Read document.xml
<?php
$zip = new ZipArchive();
if ($zip->open(__DIR__ . '/document.docx') !== true) throw new RuntimeException('Cannot open DOCX.');
$xml = $zip->getFromName('word/document.xml'); $zip->close();
if ($xml === false) throw new RuntimeException('Missing document XML.');
$dom = new DOMDocument(); $dom->loadXML($xml, LIBXML_NONET);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
foreach ($xpath->query('//w:p') as $paragraph) {
$parts = [];
foreach ($xpath->query('.//w:t', $paragraph) as $text) $parts[] = $text->textContent;
if ($parts) echo implode('', $parts) . "\n";
}Know the limits
- Treat the result as plain text, not preserved layout.
- Account for headers and footers when required.
- Use a document library for styles and tracked changes.
- Keep network access disabled while parsing untrusted XML.
Reference: PHP DOMDocument reference.