Ability to limit number of replacements performed by setValue() method of Template class
This commit is contained in:
Roman Syroeshko 2014-01-16 04:19:41 -08:00
parent 029de3183a
commit 2c5fed61bd

View File

@ -81,8 +81,9 @@ class PHPWord_Template
* *
* @param mixed $search * @param mixed $search
* @param mixed $replace * @param mixed $replace
* @param integer $limit
*/ */
public function setValue($search, $replace) public function setValue($search, $replace, $limit = -1)
{ {
$pattern = '|\$\{([^\}]+)\}|U'; $pattern = '|\$\{([^\}]+)\}|U';
preg_match_all($pattern, $this->_documentXML, $matches); preg_match_all($pattern, $this->_documentXML, $matches);
@ -102,7 +103,12 @@ class PHPWord_Template
} }
} }
$this->_documentXML = str_replace($search, $replace, $this->_documentXML); $regExpDelim = '/';
$escapedSearch = preg_quote($search, $regExpDelim);
$this->_documentXML = preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u",
$replace,
$this->_documentXML,
$limit);
} }
/** /**
@ -134,4 +140,4 @@ class PHPWord_Template
rename($this->_tempFileName, $strFilename); rename($this->_tempFileName, $strFilename);
} }
} }