Using PHP to Search and Replace POST vars in docx document <p>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. </p> <p>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.</p> <p>Any thoughts are very much appreciated. Thank you. Here is the code:</p> <pre><code>&lt;?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-&gt;open($wordDoc) === TRUE) { //Read contents into memory $oldContents = $zip-&gt;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-&gt;deleteName($fileToModify); //Write the new... $zip-&gt;addFromString($fileToModify, $newContents); //Open Footer and change vars there $ft = 'word/footer1.xml'; $oldft = $zip-&gt;getFromName($ft); $newft = str_replace('{pname}', $pname, $oldft); $zip-&gt;deleteName($ft); $zip-&gt;addFromString($ft, $newft); $zip-&gt;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(); } ?&gt; </code></pre>