Skip to content

Commit 049c89d

Browse files
committed
IMPROVED : Advanced string replace in setValue for Template
1 parent 38a15e5 commit 049c89d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Classes/PHPWord/Template.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public function __construct($strFilename) {
8484
* @param mixed $replace
8585
*/
8686
public function setValue($search, $replace) {
87+
$pattern = '|\$\{([^\}]+)\}|U';
88+
preg_match_all($pattern, $this->_documentXML, $matches);
89+
foreach ($matches[0] as $value) {
90+
$valueCleaned = preg_replace('/<[^>]+>/', '', $value);
91+
$valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
92+
$this->_documentXML = str_replace($value, $valueCleaned, $this->_documentXML);
93+
}
94+
8795
if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
8896
$search = '${'.$search.'}';
8997
}
@@ -96,6 +104,7 @@ public function setValue($search, $replace) {
96104

97105
$this->_documentXML = str_replace($search, $replace, $this->_documentXML);
98106
}
107+
99108
/**
100109
* Returns array of all variables in template
101110
*/
@@ -104,6 +113,7 @@ public function getVariables()
104113
preg_match_all('/\$\{(.*?)}/i', $this->_documentXML, $matches);
105114
return $matches[1];
106115
}
116+
107117
/**
108118
* Save Template
109119
*

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ Fixed in branch for release 0.7 :
3333
- General: (Progi1984) GH-27 - Support for Composer
3434
- General: (Progi1984) - Basic CI with Travis
3535
- General: (Progi1984) - Added PHPWord_Exception and exception when could not copy the template
36-
- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory
36+
- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory
37+
- General: (Esmeraldo) CP-49 - IMPROVED : Advanced string replace in setValue for Template

0 commit comments

Comments
 (0)