Search in This Blog

Extract text from docx in php


Problem:

Sometime you need to store Microsoft word 2007 file's content to Database, for this you will open docx file;copy it's content and paste in webpage and click submit button then after data will store in database. it is the solution but it is not a good smart solution.

Solution:

The good smart solution is that you select your docx file by browse button then your docx file will upload in server and it's content will store in Database.

Example:

<?php
function showDocxToText($file_name) {
    return readDocxToXML($file_name, "word/document.xml");
}
function readDocxToXML($file_name, $data_file) {
    $zp = new ZipArchive;
    if (true === $zp->open($file_name)) {
        if (($id = $zp->locateName($data_file)) !== false) {
            $dt = $zp->getFromIndex($id);
            $zp->close();
            $xml = DOMDocument::loadXML($dt, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            return strip_tags($xml->saveXML());
        }
        $zp->close();
    }
    return "";
}

$docx_txt=showDocxToText("file.docx");
// now store $docx_txt content to database
?>
Previous
Next Post »