Php 如何将xml转换为docx文档?
我想创建一个可以替换docx文档中特定单词的应用程序,我有一个可以将docx文件转换为xml的函数,下面是这个函数Php 如何将xml转换为docx文档?,php,docx,Php,Docx,我想创建一个可以替换docx文档中特定单词的应用程序,我有一个可以将docx文件转换为xml的函数,下面是这个函数 function read_file_docx($filename){ $content = ''; if(!$filename || !file_exists($filename)) return false; $zip = zip_open($filename); if (!$zip || is_numeric($zip)) return fal
function read_file_docx($filename){
$content = '';
if(!$filename || !file_exists($filename)) return false;
$zip = zip_open($filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}// end while
zip_close($zip);
return $content;
}
此函数返回以下xml数据
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="style0"/>
<w:spacing w:after="0" w:before="0"/>
<w:contextualSpacing w:val="false"/>
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t>Nomor</w:t>
<w:tab/>
<w:tab/>
<w:t>: 032/AP-DK/04/14</w:t>
</w:r>
</w:p>
..
诺莫
:032/AP-DK/04/14
..
有了上面的结果,我可以进行字符串处理来替换特定的单词,问题是如何将上面的xml恢复到docx文档?好吧,试着切换到类。它实际上可以在处理后将文件打包回去。特别是看。正如用户所说,它可以覆盖相同的条目。谢谢@HAL9000,但是当我尝试使用$zip->addFromString()时,它只是将xml写入docx文件,你能给我一个使用docx文件的示例吗好的,感谢@HAL9000,我只需要将$zip->addFromString()的第一个参数更改为'word/document.xml',就像这个示例一样,它工作得很好