Skip to content

Commit 18728f0

Browse files
committed
Merge branch 'feat/add-limit-to-cloneblock' of github.com:moghwan/PHPWord into feat/add-limit-to-cloneblock
2 parents ca05abb + eb5f82b commit 18728f0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ public function cloneRowAndSetValues($search, $values): void
893893
*
894894
* @return null|string
895895
*/
896-
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null)
896+
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
897897
{
898898
$xmlBlock = null;
899899
$matches = [];
@@ -921,11 +921,21 @@ public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVaria
921921
}
922922

923923
if ($replace) {
924-
$this->tempDocumentMainPart = str_replace(
925-
$matches[2] . $matches[3] . $matches[4],
926-
implode('', $cloned),
927-
$this->tempDocumentMainPart
928-
);
924+
if (self::MAXIMUM_REPLACEMENTS_DEFAULT === $limit) {
925+
$this->tempDocumentMainPart = str_replace(
926+
$matches[2] . $matches[3] . $matches[4],
927+
implode('', $cloned),
928+
$this->tempDocumentMainPart
929+
);
930+
} else {
931+
$regExpEscaper = new RegExp();
932+
$this->tempDocumentMainPart = preg_replace(
933+
$regExpEscaper->escape($matches[2] . $matches[3] . $matches[4]),
934+
implode('', $cloned),
935+
$this->tempDocumentMainPart,
936+
$limit
937+
);
938+
}
929939
}
930940
}
931941

0 commit comments

Comments
 (0)