phpword setValue 字符串替换不起作用
我有一个 PHP 应用程序,旨在将用户数据与 DOCX Word 文档合并,我使用 phpoffice/phpword 库来完成此任务。问题在于,某些变量会被不规则地替换,而大多数变量根本不会被替换。我要替换的变量不包含特殊字符、符号等,只是基本字母和数字。
模板文档
..我们公司与 propertyname 之间的合同,办事处位于地址 1、地址 2、邮政编码城市名称、国家/地区名称(“客户”)。
代码 - 选项 1:setValue 替换占位符rel="nofollow noreferrer">官方文档。
// template file
$file = '../../public/formats/contracts/'.$filename;
$phpword = new \PhpOffice\PhpWord\TemplateProcessor($file);
$phpword->setValue('propertyname', $hotel_name);
$phpword->setValue('address1', $street_1);
$phpword->setValue('address2', $street_2);
$phpword->setValue('postalcode', $zip_code);
$phpword->setValue('cityname', $city);
$phpword->setValue('countryname', $country);
$phpword->saveAs('../../public/formats/generated/'.$created_at.'_'.$filename);
结果: 没有占位符变量被替换。尝试重命名占位符(例如 ${address1}
或 %%ADDRESS_1%%
或 address_1
),但没有结果。
代码 - 选项 2: 通过 setValues 数组
一次性替换占位符
// template file
$file = '../../public/formats/contracts/'.$filename;
$phpword = new \PhpOffice\PhpWord\TemplateProcessor($file);
$phpword->setValues(array('propertyname' => $hotel_name, 'address1' => $street_1, 'address2' => $street_2, 'postalcode' => $zip_code, 'cityname' => $city, 'countryname' => $country));
$phpword->saveAs('../../public/formats/generated/'.$created_at.'_'.$filename);
结果:没有占位符变量被替换。
许多用户抱怨同样的问题,但最终通过利用记录的 phpWord ZipArchive
函数取得的成功有限 此处(5 岁)。
代码 - 选项 3: 解压 docx 文件,打开 document.xml 并直接替换其中的字符串
// Creating the new document...
$zip = new \PhpOffice\PhpWord\Shared\ZipArchive();
$document_xml = 'word/document.xml'; // docx content file
$template = '../../public/formats/contracts/'.$filename; // template file
$new_file = '../../public/formats/generated/'.$created_at.'_'.$filename; // new file after merge
// create copy of template in temporary folder
copy($template, $new_file);
if ($zip->open($new_file) === TRUE) {
$content = $zip->getFromName($document_xml); // Read contents into memory
$content = str_replace('propertyname', $hotel_name, $content); // property name
$content = str_replace('address1', $street_1, $content); // address 1
$content = str_replace('address1', $street_2, $content); // address 2
$zip->addFromString($document_xml, $content); // replace content
$return = $zip->close(); // write changes to filesystem.
if ($return == TRUE) {
$result_arr[] = array('status' => 'success', 'response' => 'Format successfully merged', 'filename' => $created_at . '_' . $filename);
}
}
结果:不定期替换一些变量。仅当占位符为 propertyname
时,变量 propertyname
才会被替换,但如果 property_name
则不会被替换。无论占位符拼写如何,变量address1
(值:“Streetname 123”)都不会被替换,但变量address2
(值:“Some District”)始终会被替换。
底线:
我确保使用最新版本的 phpWord,我在 Ubuntu 20.04 LTS 机器上运行 PHP 8.0.2 和 Apache2。替换问题似乎没有一致性,它发生不规律,有时会替换一些变量。
如果有人对这里的罪魁祸首有任何建议,我非常感谢任何帮助。否则,我在这上面花了太多时间,并且很高兴看到可以实现这一目的的替代包。
谢谢
I have a PHP application that is meant to merge user data with a DOCX word document, I am using phpoffice/phpword library for this task. The issue is that some variables are irregularly replaced while most of the variables are simply not replaced at all. The variables I am replacing with do not contain special characters, no symbols, etc. just basic letters and numbers.
Template Document
.. contract between our company and propertyname with offices at address1, address2, postalcode cityname, countryname ("Customer").
Code - Option1: Replace placeholder via setValue
as recommended in the official documentation.
// template file
$file = '../../public/formats/contracts/'.$filename;
$phpword = new \PhpOffice\PhpWord\TemplateProcessor($file);
$phpword->setValue('propertyname', $hotel_name);
$phpword->setValue('address1', $street_1);
$phpword->setValue('address2', $street_2);
$phpword->setValue('postalcode', $zip_code);
$phpword->setValue('cityname', $city);
$phpword->setValue('countryname', $country);
$phpword->saveAs('../../public/formats/generated/'.$created_at.'_'.$filename);
Result:
no placeholder variables get replaced. Tried to rename the placeholders (Eg. ${address1}
or %%ADDRESS_1%%
or address_1
) but not result.
Code - Option 2: Replace placeholders all in one go via setValues array
// template file
$file = '../../public/formats/contracts/'.$filename;
$phpword = new \PhpOffice\PhpWord\TemplateProcessor($file);
$phpword->setValues(array('propertyname' => $hotel_name, 'address1' => $street_1, 'address2' => $street_2, 'postalcode' => $zip_code, 'cityname' => $city, 'countryname' => $country));
$phpword->saveAs('../../public/formats/generated/'.$created_at.'_'.$filename);
Result: no placeholder variables get replaced.
Many users complained about the same issue and ultimately had limited success by utilising the phpWord ZipArchive
function documented here (5 years old).
Code - Option 3: Unzip docx file, open document.xml and replace strings there directly
// Creating the new document...
$zip = new \PhpOffice\PhpWord\Shared\ZipArchive();
$document_xml = 'word/document.xml'; // docx content file
$template = '../../public/formats/contracts/'.$filename; // template file
$new_file = '../../public/formats/generated/'.$created_at.'_'.$filename; // new file after merge
// create copy of template in temporary folder
copy($template, $new_file);
if ($zip->open($new_file) === TRUE) {
$content = $zip->getFromName($document_xml); // Read contents into memory
$content = str_replace('propertyname', $hotel_name, $content); // property name
$content = str_replace('address1', $street_1, $content); // address 1
$content = str_replace('address1', $street_2, $content); // address 2
$zip->addFromString($document_xml, $content); // replace content
$return = $zip->close(); // write changes to filesystem.
if ($return == TRUE) {
$result_arr[] = array('status' => 'success', 'response' => 'Format successfully merged', 'filename' => $created_at . '_' . $filename);
}
}
Result: replaces some variables irregularly. Variable propertyname
gets replaced only when the placeholder is propertyname
but not if property_name
. Variable address1
(value: "Streetname 123") never got replaced no matter the placeholder spelling, but variable address2
(value: "Some District") gets replaced all the time.
Bottom Line:
I made sure to use the latest version of phpWord, I am running PHP 8.0.2 and Apache2 on a Ubuntu 20.04 LTS machine. There seems to be no consistency for the replacement issue, it occurs irregularly and replaces some variables some time.
If anyone has any suggestions on what could be the culprit here, I much appreciate any help. Otherwise, I spent way too much time on this and am happy to look at alternative packages that can do the trick.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我创建一个新文档时,输入 2 个文本
Test1
和Test2
,然后查看“document.xml”(使用 7zip 管理器等打开文档时)我确实看到了这一部分:
重新打开文档,并将
Test2
更改为TEst2
时,document.xml 如下所示:当您使用Word 找到
TEst2
如果我们只是查看压缩文档中的“document.xml”。注意:我不知道,也没有查看过 phpword 来检查该产品如何处理上述问题。
When I create a new document, and type the 2 texts
Test1
andTest2
, and look at the "document.xml" (when opening the document using something like 7zip manager)I do see this part:
When you re-open the document using Word, and change
Test2
toTEst2
, the document.xml looks like this:It would be very hard to find
TEst2
if we are just looking at the "document.xml" from the compressed document.NOTE: I do not know, and have not looked at, phpword to check how that product deals with above problem.