Php. Как преобразовать docx в html?


На просторах великого stackoverflow нашел интересное и простое решение проблемы преобразования docx в html.

В этой короткой заметке приведу найденное решение. Оригинал — здесь.

function readDocx($filePath) {
    // Create new ZIP archive
    $zip = new ZipArchive;
    $dataFile = 'word/document.xml';
    // Open received archive file
    if (true === $zip->open($filePath)) {
        // If done, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // If found, read it to the string
            $data = $zip->getFromIndex($index);
            // Close archive file
            $zip->close();
            // Load XML from a string
            // Skip errors and warnings
            $d = new DOMDocument();
            $xml = $d->loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags
 
            $contents = explode('\n',strip_tags($d->saveXML()));
            $text = '';
            foreach($contents as $i=>$content) {
                $text .= $contents[$i];
            }           

            return $text;
        }
        $zip->close();
    }
    // In case of failure return empty string
    return "";
}

В реальном проекте код не опробовал — но на локальном сайте проверил — код рабочий и довольно шустр.

,

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *