douwang4374 2016-07-02 13:15
浏览 58

使用PHP在docx文档中搜索和替换POST变量

I am trying to do a search and replace on a Microsoft Word docx document. The odd problem I am having is that, of the 7 posted vars, 1 and 5 do not replace. In checking Chrome Tools, they all appear in the posted form data. The names are all spelled correctly too.

To explain this code, I am opening a template doc, copying it to another name (the named file which will be downloaded), replacing text in document.xml, then replacing text in the footer. Then I close the file and download it. It downloads, except, the vars $pname and $hca2name do not replace inside of the document. All other vars are properly replaced.

Any thoughts are very much appreciated. Thank you. Here is the code:

<?php
$pname = (string) $_POST['pname'];
$countyres = (string) $_POST['countyres'];
$a1name = (string) $_POST['a1name'];
$a2name = (string) $_POST['a2name'];
$hca1name = (string) $_POST['hca1name'];
$hca2name = (string) $_POST['hca2name'];
$atty = (string) $_POST['atty'];
$countyres = (string) $_POST['countyres'];
$fn = 'POA-'.$pname.'.docx';
$s = "docs_archive/PA-poas2no.docx";
$wordDoc = "POA-".$pname.".docx";
copy($s, $wordDoc);
$zip = new ZipArchive;
//This is the main document in a .docx file.
$fileToModify = 'word/document.xml';
if ($zip->open($wordDoc) === TRUE) {
    //Read contents into memory
    $oldContents = $zip->getFromName($fileToModify);
    //Modify contents:
    $newContents = str_replace('{pname}', $pname, $oldContents);
    $newContents = str_replace('{a1name}', $a1name, $newContents);
    $newContents = str_replace('{a2name}', $a2name, $newContents);
    $newContents = str_replace('{hca1name}', $hca1name, $newContents);
    $newContents = str_replace('{hca2name}', $hca2name, $newContents);
    $newContents = str_replace('{atty}', $atty, $newContents);
    $newContents = str_replace('{countyres}', $countyres, $newContents);
    //Delete the old...
    $zip->deleteName($fileToModify);
    //Write the new...
    $zip->addFromString($fileToModify, $newContents);
    //Open Footer and change vars there
    $ft = 'word/footer1.xml';
    $oldft = $zip->getFromName($ft);
    $newft = str_replace('{pname}', $pname, $oldft);
    $zip->deleteName($ft);
    $zip->addFromString($ft, $newft);
    $zip->close();
header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename="'.$fn.'"');
header('Content-Transfer-Encoding: binary');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
readfile($fn);
unlink($fn);
exit();
}
?>
  • 写回答

2条回答 默认 最新

  • duan19913 2016-07-03 09:57
    关注

    For the benefit of any future reader experiencing a similar problem, when I analyzed the document.xml file within the zipped docx file, my variable names were often not contiguous instead of {pname} it would be seperated into { pname

    Open you main document template, got to File, Inspect Document, Check for issues. This will allow you to clean up the file and remove much of that intervening xml which, I assume, is some document tracking data.

    It worked for me.

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题