php - 如何使用 php 将 docx 文档转换为 html?

标签 php html docx

我希望能够上传一个 MS word 文档并将其导出到我网站的一个页面。

有什么办法可以做到这一点吗?

最佳答案

//FUNCTION :: read a docx file and return the string
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
            $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags

            $contents = explode('\n',strip_tags($xml->saveXML()));
            $text = '';
            foreach($contents as $i=>$content) {
                $text .= $contents[$i];
            }
            return $text;
        }
        $zip->close();
    }
    // In case of failure return empty string
    return "";
}

ZipArchiveDOMDocument 都在 PHP 中,因此您不需要安装/包含/需要额外的库。

关于php - 如何使用 php 将 docx 文档转换为 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4587216/

相关文章:

javascript - 在javascript中返回HTML代码?

javascript - 如何检查一个事件是最后一个事件?

python - 如何使用 python docx 在上标或下标中添加文本

ios - 在 iOS 中创建 RTF 、 DOC 或 DOCX

javascript - 我可以将带有文件输入元素的 WebRTC 音频 blob 对象作为普通文件上传但不下载吗?

php - 代码点火器 “The requested URL was not found” 错误

php - Fwrite() 和 Fread() 与 Mysql 数据库

javascript - 如何使用 jQuery 获取内部样式表?

python-docx : How to printout the page number and the total page of the document?

foreach 循环中的 PHP JSON 编码