From 170cdff636182c077308494b55ed7ef8acdb5382 Mon Sep 17 00:00:00 2001 From: Chalda Pnuzig <16133710+chalda-pnuzig@users.noreply.github.com> Date: Sun, 23 Feb 2025 17:48:47 +0100 Subject: [PATCH] Fix text extraction from PhpWord TextRun elements Previously, `$title->getText()` was assumed to always return a string, but in some cases, it returns a `PhpOffice\PhpWord\Element\TextRun`. This commit adds a check to determine if the returned value is a `TextRun` and properly extracts text from its child elements. --- src/PhpWord/Writer/Word2007/Element/TOC.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 44c6ba11fa..c6728b4900 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -96,7 +96,17 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti $xmlWriter->startElement('w:t'); $titleText = $title->getText(); - $this->writeText(is_string($titleText) ? $titleText : ''); + if (get_class($titleText) === 'PhpOffice\PhpWord\Element\TextRun') { + $textRunElements = $title->getText()->getElements(); + $uploadedText = ''; + foreach ($textRunElements as $textRunElement) { + $uploadedText .= $textRunElement->getText(); + $uploadedText .= ' '; + } + $this->writeText($uploadedText); + } else { + $this->writeText((is_string($titleText) ? $titleText : ''); + } $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r