- trim: documentation ( source)
<?php
$html = '<!DOCTYPE html>
<title>Valid HTML5 Document</title>
<p>Paragraph 1</p>
<script>console.log("</html>Console log text");</script>
<p>Paragraph 2</p>';
// DOM/HTMLDocument (lexbor)
$newDom = \DOM\HTMLDocument::createFromString($html);
$paragraphs = $newDom->getElementsByTagName('p');
echo "\n";
echo "DOM\HTMLDocument parsing (lexbor)\n";
echo "---------------------------------\n";
echo "{$paragraphs->length} paragraph elements found.\n";
// 2 paragraphs found (correct)
foreach ($paragraphs as $p) {
echo " * " . trim($p->textContent) . "\n";
}
echo "\n";
echo "DOM serialised:\n";
echo $newDom->saveHtml();