- 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 = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->loadHtml($html);
$paragraphs = $dom->getElementsByTagName('p');
echo "DOMDocument parsing (libxml)\n";
echo "----------------------------\n";
echo "{$paragraphs->length} paragraph elements found:\n";
// 3 paragraphs found but we expected 2
foreach ($paragraphs as $p) {
echo " * " . trim($p->textContent) . "\n";
}
echo "\n";
echo "DOM serialised:\n";
echo $dom->saveHtml();