From 36e5ecb918a244464aa7616f590752f0767af0d2 Mon Sep 17 00:00:00 2001 From: rasamassen Date: Sat, 13 Sep 2025 00:01:42 -0500 Subject: [PATCH 1/4] Create PreserveText.php --- .../Writer/RTF/Element/PreserveText.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/PhpWord/Writer/RTF/Element/PreserveText.php diff --git a/src/PhpWord/Writer/RTF/Element/PreserveText.php b/src/PhpWord/Writer/RTF/Element/PreserveText.php new file mode 100644 index 0000000000..7506257713 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/PreserveText.php @@ -0,0 +1,60 @@ +element; + if (!$element instanceof \PhpOffice\PhpWord\Element\PreserveText) { + return ''; + } + + $this->getStyles(); + + $content = ''; + $content .= $this->writeOpening(); + $content .= '{'; + $content .= $this->writeFontStyle(); + foreach($element->getText() as $text) { + if (preg_match('/[{}]/', $text) == 1) { + $text = str_replace(array('{', '}'), '', $text); + $content .= '{\field {\*\fldinst {' . $text . '}}{\\fldrslt {}}}'; + } else { + $content .= $this->writeText($text); + } + } + $content .= '}'; + $content .= $this->writeClosing(); + + return $content; + } +} From eb1fc7f82edbea96b22380ec43e17249b476dd67 Mon Sep 17 00:00:00 2001 From: rasamassen Date: Sat, 13 Sep 2025 00:04:09 -0500 Subject: [PATCH 2/4] Update 1.5.0.md - Changelog for Pull 2826 --- docs/changes/1.x/1.5.0.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index b96865bada..f4620c201b 100644 --- a/docs/changes/1.x/1.5.0.md +++ b/docs/changes/1.x/1.5.0.md @@ -7,6 +7,7 @@ ### Bug fixes - Set writeAttribute return type by [@radarhere](https://github.com/radarhere) fixing [#2204](https://github.com/PHPOffice/PHPWord/issues/2204) in [#2776](https://github.com/PHPOffice/PHPWord/pull/2776) +- Writer RTF: Create PreserveText by [@rasamassen](https://github.com/rasamassen) in [#2826](https://github.com/PHPOffice/PHPWord/pull/2826) ### Miscellaneous @@ -16,4 +17,4 @@ ### BC Breaks -### Notes \ No newline at end of file +### Notes From 6848f80f3462467586fc3955bd4294dd888d0678 Mon Sep 17 00:00:00 2001 From: rasamassen Date: Sat, 13 Sep 2025 00:07:57 -0500 Subject: [PATCH 3/4] Update PreserveText.php - Static and CS Fixes --- src/PhpWord/Writer/RTF/Element/PreserveText.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/PhpWord/Writer/RTF/Element/PreserveText.php b/src/PhpWord/Writer/RTF/Element/PreserveText.php index 7506257713..3822794619 100644 --- a/src/PhpWord/Writer/RTF/Element/PreserveText.php +++ b/src/PhpWord/Writer/RTF/Element/PreserveText.php @@ -44,13 +44,17 @@ public function write() $content .= $this->writeOpening(); $content .= '{'; $content .= $this->writeFontStyle(); - foreach($element->getText() as $text) { - if (preg_match('/[{}]/', $text) == 1) { - $text = str_replace(array('{', '}'), '', $text); - $content .= '{\field {\*\fldinst {' . $text . '}}{\\fldrslt {}}}'; - } else { - $content .= $this->writeText($text); + if(is_array($element->getText())) { + foreach ($element->getText() as $text) { + if (preg_match('/[{}]/', $text) == 1) { + $text = str_replace(['{', '}'], '', $text); + $content .= '{\field {\*\fldinst {' . $text . '}}{\\fldrslt {}}}'; + } else { + $content .= $this->writeText($text); + } } + } else { + $content .= $this->writeText($element->getText()); } $content .= '}'; $content .= $this->writeClosing(); From c58ea5e2bb65895a23fdbffbc775670576680d00 Mon Sep 17 00:00:00 2001 From: rasamassen Date: Sat, 13 Sep 2025 00:09:34 -0500 Subject: [PATCH 4/4] Update PreserveText.php - CS Fix --- src/PhpWord/Writer/RTF/Element/PreserveText.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/RTF/Element/PreserveText.php b/src/PhpWord/Writer/RTF/Element/PreserveText.php index 3822794619..5422f0d8fa 100644 --- a/src/PhpWord/Writer/RTF/Element/PreserveText.php +++ b/src/PhpWord/Writer/RTF/Element/PreserveText.php @@ -44,7 +44,7 @@ public function write() $content .= $this->writeOpening(); $content .= '{'; $content .= $this->writeFontStyle(); - if(is_array($element->getText())) { + if (is_array($element->getText())) { foreach ($element->getText() as $text) { if (preg_match('/[{}]/', $text) == 1) { $text = str_replace(['{', '}'], '', $text);