Skip to content

Commit c5944f1

Browse files
🩹 Remove XML declaration and comments from HTML output in Document class.
1 parent 708b689 commit c5944f1

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

‎src/Document.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ public function xpath(string $expression): DOMNodeList
6969
*/
7070
public function html(): string
7171
{
72-
return trim(substr($this->document->saveHTML(), 23));
72+
$html = $this->document->saveHTML();
73+
74+
// Remove XML declaration/comment that libxml2 may add
75+
// Handles both <?xml encoding="UTF-8"> and any comments libxml2 adds
76+
$html = preg_replace('/^<\?xml[^>]*>\s*/i', '', $html);
77+
$html = preg_replace('/^<!--[^>]*-->\s*/', '', $html);
78+
79+
return trim($html);
7380
}
7481

7582
/**

0 commit comments

Comments
 (0)