@@ -82,16 +82,16 @@ 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 ) {
85
+ while (false !== $ this ->zipClass ->locateName ($ this ->getHeaderName ($ index ))) {
86
86
$ this ->temporaryDocumentHeaders [$ index ] = $ this ->zipClass ->getFromName ($ this ->getHeaderName ($ index ));
87
87
$ index ++;
88
88
}
89
89
$ index = 1 ;
90
- while ($ this ->zipClass ->locateName ($ this ->getFooterName ($ index )) !== false ) {
90
+ while (false !== $ this ->zipClass ->locateName ($ this ->getFooterName ($ index ))) {
91
91
$ this ->temporaryDocumentFooters [$ index ] = $ this ->zipClass ->getFromName ($ this ->getFooterName ($ index ));
92
92
$ index ++;
93
93
}
94
- $ this ->temporaryDocumentMainPart = $ this ->zipClass ->getFromName ('word/document.xml ' );
94
+ $ this ->tempDocumentMainPart = $ this ->fixBrokenMacros ( $ this -> zipClass ->getFromName ('word/document.xml ' ) );
95
95
}
96
96
97
97
/**
@@ -175,7 +175,7 @@ public function getVariables()
175
175
*/
176
176
public function cloneRow ($ search , $ numberOfClones )
177
177
{
178
- if (substr ($ search , 0 , 2 ) !== ' ${ ' && substr ($ search , -1 ) !== ' } ' ) {
178
+ if (' ${ ' !== substr ($ search , 0 , 2 ) && ' } ' !== substr ($ search , -1 )) {
179
179
$ search = '${ ' . $ search . '} ' ;
180
180
}
181
181
@@ -345,6 +345,31 @@ public function saveAs($fileName)
345
345
unlink ($ tempFileName );
346
346
}
347
347
348
+ /**
349
+ * Finds parts of broken macros and sticks them together.
350
+ * Macros, while being edited, could be implicitly broken by some of the word processors.
351
+ *
352
+ * @since 0.13.0
353
+ *
354
+ * @param string $documentPart The document part in XML representation.
355
+ *
356
+ * @return string
357
+ */
358
+ protected function fixBrokenMacros ($ documentPart )
359
+ {
360
+ $ fixedDocumentPart = $ documentPart ;
361
+
362
+ $ fixedDocumentPart = preg_replace_callback (
363
+ '|\$\{([^\}]+)\}|U ' ,
364
+ function ($ match ) {
365
+ return strip_tags ($ match [0 ]);
366
+ },
367
+ $ fixedDocumentPart
368
+ );
369
+
370
+ return $ fixedDocumentPart ;
371
+ }
372
+
348
373
/**
349
374
* Find and replace placeholders in the given XML section.
350
375
*
@@ -356,14 +381,6 @@ public function saveAs($fileName)
356
381
*/
357
382
protected function setValueForPart ($ documentPartXML , $ search , $ replace , $ limit )
358
383
{
359
- $ pattern = '|\$\{([^\}]+)\}|U ' ;
360
- preg_match_all ($ pattern , $ documentPartXML , $ matches );
361
- foreach ($ matches [0 ] as $ value ) {
362
- $ valueCleaned = preg_replace ('/<[^>]+>/ ' , '' , $ value );
363
- $ valueCleaned = preg_replace ('/<\/[^>]+>/ ' , '' , $ valueCleaned );
364
- $ documentPartXML = str_replace ($ value , $ valueCleaned , $ documentPartXML );
365
- }
366
-
367
384
if (substr ($ search , 0 , 2 ) !== '${ ' && substr ($ search , -1 ) !== '} ' ) {
368
385
$ search = '${ ' . $ search . '} ' ;
369
386
}
0 commit comments