nodeToText($doc->getBody()); // Remove repeated newlines $text = preg_replace('/\n+/', "\n", $text); // Remove leading/trailing whitespace $text = trim($text); return $text; } protected function nodeToText(\DOMNode $node): string { if ($node->nodeType === XML_TEXT_NODE) { return $node->textContent; } $text = ''; if (!in_array($node->nodeName, $this->inlineTags)) { $text .= "\n"; } foreach ($node->childNodes as $childNode) { $text .= $this->nodeToText($childNode); } return $text; } }