I want to replace multiple words in my docx-file. I used the words from the input-elements and I pass them through the 'POST'-method. I already replaced the '$bedrijfsnaam' but I want to add more str replacements. I created '$newContents2' but as I thought, it didn't work. How can I solve this? Do I have to add another 'oldContents' like 'oldContents2'?
$bedrijfsnaam = $_POST['bedrijfsnaam'];
$offertenummer = $_POST['offertenummer'];
$naam = $_POST['naam'];
$zip = new ZipArchive;
//This is the main document in a .docx file.
$fileToModify = 'word/document.xml';
$wordDoc = "Document.docx";
$newFile = $offertenummer . ".docx";
copy("Document.docx", $newFile);
if ($zip->open($newFile) === TRUE) {
$oldContents = $zip->getFromName($fileToModify);
$newContents = str_replace('$bedrijfsnaam', $bedrijfsnaam, $oldContents);
$newContents2 = str_replace('$naam', $naam, $oldContents);
$zip->deleteName($fileToModify);
$zip->addFromString($fileToModify, $newContents);
$return =$zip->close();
If ($return==TRUE){
echo "Success!";
}
} else {
echo 'failed';
}
$newFilePath = 'offerte/' . $newFile;
$fileMoved = rename($newFile, $newFilePath);
'$bedrijfsnaam'is that literal value, dont quote variables. You can use arrays for the search and replace values, see the manual (php.net/manual/en/function.str-replace.php).