Page 1 of 1

PhpWord SetValue -Zeichenfolge Ersatz funktioniert nicht

Posted: 16 Aug 2025, 22:45
by Anonymous
Ich habe eine PHP -Anwendung, die Benutzerdaten mit einem DOCX -Word -Dokument zusammenführen soll. Ich verwende die PhpOFFICE/PHPWord -Bibliothek für diese Aufgabe. Das Problem ist, dass einige Variablen unregelmäßig ersetzt werden, während die meisten Variablen einfach nicht ersetzt werden. Die Variablen, die ich ersetze, enthalten keine Sonderzeichen, keine Symbole usw. Nur grundlegende Buchstaben und Zahlen. Option1: Ersetzen Sie den Platzhalter durch SetValue wie in der offiziellen Dokumentation empfohlen.

Code: Select all

// 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);
< /code>
Ergebnis:
Keine Platzhaltervariablen werden ersetzt. Versucht, die Platzhalter umzubenennen (z. B. $ {adress1} 
oder %% adress_1 %% oder addres_1 ), aber nicht Ergebnis.

Code: Select all

// 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);
Ergebnis: Keine Platzhaltervariablen werden ersetzt. Direkt < /p>

Code: Select all

// 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);
}
}
< /code>
Ergebnis: Ersetzt einige Variablen unregelmäßig. Variable PropertyName 
wird nur ersetzt, wenn der Platzhalter PropertyName ist, jedoch nicht, wenn Property_Name . Variable address1 (Wert: "StreetName 123") wurde nie ersetzt, unabhängig von der Schreibweise der Platzhalter, aber Variable Adress2 (Wert: "Ein Distrikt") wird ständig ersetzt. LTS -Maschine. Es scheint keine Konsistenz für das Ersatzproblem zu geben, es tritt unregelmäßig auf und ersetzt einige Variablen einige Zeit. Andernfalls habe ich viel zu viel Zeit damit verbracht und freue mich, alternative Pakete zu betrachten, die den Trick machen können.
Danke