From 049c89d0790e14186877997c4aaaee1e9cab8633 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Sun, 15 Dec 2013 14:46:05 +0100 Subject: [PATCH] IMPROVED : Advanced string replace in setValue for Template --- Classes/PHPWord/Template.php | 10 ++++++++++ changelog.txt | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Template.php b/Classes/PHPWord/Template.php index bbc3197f..dedc8f9c 100644 --- a/Classes/PHPWord/Template.php +++ b/Classes/PHPWord/Template.php @@ -84,6 +84,14 @@ class PHPWord_Template { * @param mixed $replace */ public function setValue($search, $replace) { + $pattern = '|\$\{([^\}]+)\}|U'; + preg_match_all($pattern, $this->_documentXML, $matches); + foreach ($matches[0] as $value) { + $valueCleaned = preg_replace('/<[^>]+>/', '', $value); + $valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned); + $this->_documentXML = str_replace($value, $valueCleaned, $this->_documentXML); + } + if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${'.$search.'}'; } @@ -96,6 +104,7 @@ class PHPWord_Template { $this->_documentXML = str_replace($search, $replace, $this->_documentXML); } + /** * Returns array of all variables in template */ @@ -104,6 +113,7 @@ class PHPWord_Template { preg_match_all('/\$\{(.*?)}/i', $this->_documentXML, $matches); return $matches[1]; } + /** * Save Template * diff --git a/changelog.txt b/changelog.txt index d614ffaf..4b0e6edc 100644 --- a/changelog.txt +++ b/changelog.txt @@ -33,4 +33,5 @@ Fixed in branch for release 0.7 : - General: (Progi1984) GH-27 - Support for Composer - General: (Progi1984) - Basic CI with Travis - General: (Progi1984) - Added PHPWord_Exception and exception when could not copy the template -- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory \ No newline at end of file +- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory +- General: (Esmeraldo) CP-49 - IMPROVED : Advanced string replace in setValue for Template \ No newline at end of file