Skip to content

Commit 8dc276a

Browse files
author
Roman Syroeshko
committed
Merged #513.
1 parent 7c371e1 commit 8dc276a

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ public function __construct($documentTemplate)
8282
$this->zipClass = new ZipArchive();
8383
$this->zipClass->open($this->temporaryDocumentFilename);
8484
$index = 1;
85-
while ($this->zipClass->locateName($this->getHeaderName($index)) !== false) {
85+
while (false !== $this->zipClass->locateName($this->getHeaderName($index))) {
8686
$this->temporaryDocumentHeaders[$index] = $this->zipClass->getFromName($this->getHeaderName($index));
8787
$index++;
8888
}
8989
$index = 1;
90-
while ($this->zipClass->locateName($this->getFooterName($index)) !== false) {
90+
while (false !== $this->zipClass->locateName($this->getFooterName($index))) {
9191
$this->temporaryDocumentFooters[$index] = $this->zipClass->getFromName($this->getFooterName($index));
9292
$index++;
9393
}
94-
$this->temporaryDocumentMainPart = $this->zipClass->getFromName('word/document.xml');
94+
$this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml'));
9595
}
9696

9797
/**
@@ -175,7 +175,7 @@ public function getVariables()
175175
*/
176176
public function cloneRow($search, $numberOfClones)
177177
{
178-
if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
178+
if ('${' !== substr($search, 0, 2) && '}' !== substr($search, -1)) {
179179
$search = '${' . $search . '}';
180180
}
181181

@@ -345,6 +345,31 @@ public function saveAs($fileName)
345345
unlink($tempFileName);
346346
}
347347

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+
348373
/**
349374
* Find and replace placeholders in the given XML section.
350375
*
@@ -356,14 +381,6 @@ public function saveAs($fileName)
356381
*/
357382
protected function setValueForPart($documentPartXML, $search, $replace, $limit)
358383
{
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-
367384
if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
368385
$search = '${' . $search . '}';
369386
}

0 commit comments

Comments
 (0)