我如何使用PHP将docx文档转换为html? [英] How can I convert a docx document to html using php?
本文介绍了我如何使用PHP将docx文档转换为html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够上传MS Word文档并将其导出到我的网站中。
有什么方法可以实现这一目标?
解决方案
// FUNCTION ::读取docx文件并返回字符串
function readDocx $ filePath){
//创建新的ZIP存档
$ zip = new ZipArchive;
$ dataFile ='word / document.xml';
//打开收到的归档文件
if(true === $ zip-> open($ filePath)){
//如果完成,搜索存档中的数据文件
if(($ index = $ zip-> locateName($ dataFile))!== false){
//如果找到,请将其读入字符串
$ data = $ zip- > getFromIndex($指数);
//关闭归档文件
$ zip-> close();
//从字符串加载XML
//跳过错误和警告
$ xml = DOMDocument :: loadXML($ data,LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
//返回没有XML格式标签的数据
$ contents = explode('\\\
',strip_tags($ xml-> saveXML()));
$ text ='';
foreach($ contents as $ i => $ content){
$ text。= $ contents [$ i];
}
返回$ text;
}
$ zip-> close();
}
//如果失败返回空字符串
return;
}
ZipArchive 和 DOMDocument 都在PHP内部,所以你不需要安装/包含/需要额外的库。
I want to be able to upload an MS word document and export it a page in my site.
Is there any way to accomplish this?
解决方案
//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 "";
}
ZipArchive and DOMDocument are both inside PHP so you don't need to install/include/require additional libraries.
这篇关于我如何使用PHP将docx文档转换为html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文