Skip to content

Commit 2d3725c

Browse files
committed
Merge pull request #85 from RomanSyroeshko/PHPWord#52
Ability to limit number of replacements performed by setValue() method of Template class
2 parents c173a71 + 2c5fed6 commit 2d3725c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Classes/PHPWord/Template.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public function __construct($strFilename)
8181
*
8282
* @param mixed $search
8383
* @param mixed $replace
84+
* @param integer $limit
8485
*/
85-
public function setValue($search, $replace)
86+
public function setValue($search, $replace, $limit = -1)
8687
{
8788
$pattern = '|\$\{([^\}]+)\}|U';
8889
preg_match_all($pattern, $this->_documentXML, $matches);
@@ -102,7 +103,12 @@ public function setValue($search, $replace)
102103
}
103104
}
104105

105-
$this->_documentXML = str_replace($search, $replace, $this->_documentXML);
106+
$regExpDelim = '/';
107+
$escapedSearch = preg_quote($search, $regExpDelim);
108+
$this->_documentXML = preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u",
109+
$replace,
110+
$this->_documentXML,
111+
$limit);
106112
}
107113

108114
/**

0 commit comments

Comments
 (0)