@@ -82,16 +82,20 @@ public function __construct($documentTemplate)
82
82
$ this ->zipClass = new ZipArchive ();
83
83
$ this ->zipClass ->open ($ this ->temporaryDocumentFilename );
84
84
$ index = 1 ;
85
- while ($ this ->zipClass ->locateName ($ this ->getHeaderName ($ index )) !== false ) {
86
- $ this ->temporaryDocumentHeaders [$ index ] = $ this ->zipClass ->getFromName ($ this ->getHeaderName ($ index ));
85
+ while (false !== $ this ->zipClass ->locateName ($ this ->getHeaderName ($ index ))) {
86
+ $ this ->temporaryDocumentHeaders [$ index ] = $ this ->fixBrokenMacros (
87
+ $ this ->zipClass ->getFromName ($ this ->getHeaderName ($ index ))
88
+ );
87
89
$ index ++;
88
90
}
89
91
$ index = 1 ;
90
- while ($ this ->zipClass ->locateName ($ this ->getFooterName ($ index )) !== false ) {
91
- $ this ->temporaryDocumentFooters [$ index ] = $ this ->zipClass ->getFromName ($ this ->getFooterName ($ index ));
92
+ while (false !== $ this ->zipClass ->locateName ($ this ->getFooterName ($ index ))) {
93
+ $ this ->temporaryDocumentFooters [$ index ] = $ this ->fixBrokenMacros (
94
+ $ this ->zipClass ->getFromName ($ this ->getFooterName ($ index ))
95
+ );
92
96
$ index ++;
93
97
}
94
- $ this ->temporaryDocumentMainPart = $ this ->zipClass ->getFromName ('word/document.xml ' );
98
+ $ this ->temporaryDocumentMainPart = $ this ->fixBrokenMacros ( $ this -> zipClass ->getFromName ('word/document.xml ' ) );
95
99
}
96
100
97
101
/**
@@ -175,7 +179,7 @@ public function getVariables()
175
179
*/
176
180
public function cloneRow ($ search , $ numberOfClones )
177
181
{
178
- if (substr ($ search , 0 , 2 ) !== ' ${ ' && substr ($ search , -1 ) !== ' } ' ) {
182
+ if (' ${ ' !== substr ($ search , 0 , 2 ) && ' } ' !== substr ($ search , -1 )) {
179
183
$ search = '${ ' . $ search . '} ' ;
180
184
}
181
185
@@ -338,6 +342,30 @@ public function saveAs($fileName)
338
342
rename ($ tempFileName , $ fileName );
339
343
}
340
344
345
+ /**
346
+ * Finds parts of broken macros and sticks them together.
347
+ * Macros, while being edited, could be implicitly broken by some of the word processors.
348
+ *
349
+ * @since 0.13.0
350
+ *
351
+ * @param string $documentPart The document part in XML representation.
352
+ *
353
+ * @return string
354
+ */
355
+ protected function fixBrokenMacros ($ documentPart ) {
356
+ $ fixedDocumentPart = $ documentPart ;
357
+
358
+ $ pattern = '|\$\{([^\}]+)\}|U ' ;
359
+ preg_match_all ($ pattern , $ fixedDocumentPart , $ matches );
360
+ foreach ($ matches [0 ] as $ value ) {
361
+ $ valueCleaned = preg_replace ('/<[^>]+>/ ' , '' , $ value );
362
+ $ valueCleaned = preg_replace ('/<\/[^>]+>/ ' , '' , $ valueCleaned );
363
+ $ fixedDocumentPart = str_replace ($ value , $ valueCleaned , $ fixedDocumentPart );
364
+ }
365
+
366
+ return $ fixedDocumentPart ;
367
+ }
368
+
341
369
/**
342
370
* Find and replace placeholders in the given XML section.
343
371
*
@@ -349,14 +377,6 @@ public function saveAs($fileName)
349
377
*/
350
378
protected function setValueForPart ($ documentPartXML , $ search , $ replace , $ limit )
351
379
{
352
- $ pattern = '|\$\{([^\}]+)\}|U ' ;
353
- preg_match_all ($ pattern , $ documentPartXML , $ matches );
354
- foreach ($ matches [0 ] as $ value ) {
355
- $ valueCleaned = preg_replace ('/<[^>]+>/ ' , '' , $ value );
356
- $ valueCleaned = preg_replace ('/<\/[^>]+>/ ' , '' , $ valueCleaned );
357
- $ documentPartXML = str_replace ($ value , $ valueCleaned , $ documentPartXML );
358
- }
359
-
360
380
if (substr ($ search , 0 , 2 ) !== '${ ' && substr ($ search , -1 ) !== '} ' ) {
361
381
$ search = '${ ' . $ search . '} ' ;
362
382
}
0 commit comments