How to extract text from word file .doc,docx,.xlsx,.pptx php

How to extract text from word file .doc,docx,.xlsx,.pptx php

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Text from Office Files for Database Search in PHP

When building applications that process user-uploaded resumes or documents, a common requirement is to extract raw text from files like .doc, .docx, .xlsx, and .pptx immediately upon upload. The goal isn't just to display the file; it's to parse the content into a searchable format stored in your database. While libraries exist for these tasks, selecting the right approach depends heavily on the specific file format and whether you need to preserve layout or simply retrieve the text string.

Handling Legacy .doc Files

The most challenging scenario often involves legacy .doc files created with Microsoft Word 97–2003. These files are binary formats based on the OLE (Object Linking and Embedding) compound document structure, not XML. In PHP, you cannot simply read these as text streams or use standard parsers.

To extract text from a .doc file, you generally have two paths: convert it first or use a specialized library. The conversion approach often involves using libreoffice-headless via a shell command if your server environment allows it. This converts the binary .doc into an XML-based format (like .xml or .txt) that is easier to parse. However, relying on external binaries can introduce dependency management issues in shared hosting environments.

Alternatively, you can use the phpoffice/phpspreadsheet library for newer formats, but for pure .doc extraction without dependencies, some developers opt for COM objects if running on Windows with Microsoft Office installed. On Linux or macOS, this is not an option without WSL or Docker containers. A more portable solution often involves using the clue/phar-io composer package to handle the conversion logic internally before processing.

Parsing Modern .docx and .pptx Files

Modern Office formats like .docx, .xlsx, and .pptx are actually ZIP archives containing XML files. This architecture makes them significantly easier to parse in PHP compared to their legacy counterparts. The standard library for this is phpoffice/phpspreadsheet, which handles the complexity of unzipping, reading the specific XML namespaces (like word/document.xml for text), and reconstructing the content.

Here is a basic implementation pattern using phpspreadsheet:

use PhpOffice\PhpSpreadsheet\IOFactory;

function extractTextFromDocx(string $filePath): string {
    $spreadsheet = IOFactory::load($filePath);
    $worksheet = $spreadsheet->getActiveSheet();
    
    // Iterate through rows and cells to build a text stream
    $textBuffer = '';
    foreach ($worksheet->getRowIterator() as $row) {
        foreach ($row->getCellIterator(false) as $cell) {
            if ($cell->getValue()) {
                $textBuffer .= (string)$cell->getValue() . ' ';
            }
        }
    }
    return trim($textBuffer);
}

For .xlsx files, the logic is similar but applies to worksheets rather than sheets. You must iterate through each Worksheet object in the spreadsheet instance. For .pptx files, you need to access the slide objects within the presentation and then their text boxes or shapes. The library abstracts this away, allowing you to loop through slides and retrieve the text from individual text runs.

Storing Extracted Data for Search

The core value proposition here is moving the text from the binary file into a relational database. Once extracted via the method above, you should sanitize the string (removing excessive whitespace, normalizing newlines) and insert it into a documents table.

CREATE TABLE documents (
    id INT AUTO_INCREMENT PRIMARY KEY,
    original_filename VARCHAR(255),
    file_path VARCHAR(255),
    extracted_text TEXT, -- Or use LONGTEXT for large content
    search_vector TEXT  -- For full-text search indexes
);

By storing extracted_text in the database, you enable efficient SQL-based searching (LIKE, FULLTEXT) rather than reading every uploaded binary file on every query. This drastically improves response times when searching resumes or documents.

Trade-offs and Pitfalls

Be aware of several pitfalls. First, formatting is often lost during extraction. A table in a Word document might become a single line of text in the extracted string, making context harder to interpret if you rely solely on the raw string. Second, security is paramount; never execute external conversion commands (like libreoffice) without strict input validation and sandboxing, as this opens your server to arbitrary command execution risks.

If you are using a SaaS provider or specific hosting restrictions prevent installing large PHP libraries, consider cloud-based APIs, though they introduce latency and cost per request. For most self-hosted applications, sticking with phpspreadsheet for modern formats and carefully managed conversion tools for legacy .doc files provides the best balance of stability and performance.

Our network