@@ -853,46 +853,38 @@ public function cloneRowAndSetValues($search, $values): void
853
853
* @param bool $replace
854
854
* @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
855
855
* @param array $variableReplacements Array containing replacements for macros found inside the block to clone
856
- *
857
- * @return null|string
858
856
*/
859
- public function cloneBlock ($ blockname , $ clones = 1 , $ replace = true , $ indexVariables = false , $ variableReplacements = null )
857
+ public function cloneBlock ($ blockname , $ clones = 1 , $ replace = true , $ indexVariables = false , $ variableReplacements = null ): void
860
858
{
861
- $ xmlBlock = null ;
862
- $ matches = [];
863
- $ escapedMacroOpeningChars = self ::$ macroOpeningChars ;
864
- $ escapedMacroClosingChars = self ::$ macroClosingChars ;
865
- preg_match (
866
- //'/(.*((?s)<w:p\b(?:(?!<w:p\b).)*?\{{' . $blockname . '}<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)[^$]*?\{{\/' . $blockname . '}<\/w:.*?p>)/is',
867
- '/(.*((?s)<w:p\b(?:(?!<w:p\b).)*? \\' . $ escapedMacroOpeningChars . $ blockname . $ escapedMacroClosingChars . '<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)[^$]*? \\' . $ escapedMacroOpeningChars . '\/ ' . $ blockname . $ escapedMacroClosingChars . '<\/w:.*?p>)/is ' ,
868
- //'/(.*((?s)<w:p\b(?:(?!<w:p\b).)*?\\'. $escapedMacroOpeningChars . $blockname . '}<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)[^$]*?\\'.$escapedMacroOpeningChars.'\/' . $blockname . '}<\/w:.*?p>)/is',
869
- $ this ->tempDocumentMainPart ,
870
- $ matches
871
- );
859
+ $ open = preg_quote (self ::ensureMacroCompleted ($ blockname ));
860
+ $ close = str_replace ('/ ' , '\/ ' , preg_quote (self ::ensureMacroCompleted ("/ $ blockname " )));
872
861
873
- if (isset ($ matches [3 ])) {
874
- $ xmlBlock = $ matches [3 ];
862
+ $ beginRe = '(<w:p\b(?:(?!<w:p\b).)*? ' . $ open . '.*?<\/w:p>) ' ;
863
+ $ endRe = '(<w:p\b(?:(?!<w:p\b).)*? ' . $ close . '.*?<\/w:p>) ' ;
864
+ $ betweenRe = '.*?(<w:p\b(?:(?!<w:p\b).)*?.*?<\/w:p>).*? ' ;
865
+ $ re = "/ $ beginRe$ betweenRe$ endRe/is " ;
866
+
867
+ $ blockMatches = [];
868
+ preg_match_all ($ re , $ this ->tempDocumentMainPart , $ blockMatches , \PREG_SET_ORDER );
869
+
870
+ foreach ($ blockMatches as $ matches ) {
871
+ $ xmlBlock = $ matches [2 ];
875
872
if ($ indexVariables ) {
876
873
$ cloned = $ this ->indexClonedVariables ($ clones , $ xmlBlock );
877
874
} elseif ($ variableReplacements !== null && is_array ($ variableReplacements )) {
878
875
$ cloned = $ this ->replaceClonedVariables ($ variableReplacements , $ xmlBlock );
879
876
} else {
880
- $ cloned = [];
881
- for ($ i = 1 ; $ i <= $ clones ; ++$ i ) {
882
- $ cloned [] = $ xmlBlock ;
883
- }
877
+ $ cloned = array_fill (0 , max (0 , $ clones ), $ xmlBlock );
884
878
}
885
879
886
880
if ($ replace ) {
887
881
$ this ->tempDocumentMainPart = str_replace (
888
- $ matches [2 ] . $ matches [ 3 ] . $ matches [ 4 ],
882
+ $ matches [0 ],
889
883
implode ('' , $ cloned ),
890
884
$ this ->tempDocumentMainPart
891
885
);
892
886
}
893
887
}
894
-
895
- return $ xmlBlock ;
896
888
}
897
889
898
890
/**
0 commit comments