【发布时间】:2017-12-10 23:11:24
【问题描述】:
我想替换我的 docx 文件中的多个单词。我使用了输入元素中的单词,并通过“POST”方法传递它们。我已经替换了 '$bedrijfsnaam' 但我想添加更多 str 替换。我创建了“$newContents2”,但正如我所想,它没有用。我该如何解决这个问题?我是否必须添加另一个“oldContents”,例如“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'是那个字面值,不要引用变量。您可以使用数组来搜索和替换值,请参阅手册 (php.net/manual/en/function.str-replace.php)。 -
还要注意双引号字符串中的美元符号 - php 将尝试解析变量并将文本替换为变量 (php.net/manual/en/…)
标签: php