3v4l.org

run code in 300+ PHP versions simultaneously
<?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();
Output for 8.2.25 - 8.2.26, 8.3.5 - 8.3.15, 8.4.1 - 8.4.2
Warning: DOMDocument::loadHTML(): Unexpected end tag : script in Entity, line: 4 in /in/OkmpO on line 10 DOMDocument parsing (libxml) ---------------------------- 3 paragraph elements found: * Paragraph 1 * Console log text"); * Paragraph 2 DOM serialised: <!DOCTYPE html> <html> <head> <title>Valid HTML5 Document</title> </head> <body> <p>Paragraph 1</p> <script>console.log("</script> </body> </html><html> <p>Console log text"); </p> <p>Paragraph 2</p> </html>

preferences:
54.28 ms | 407 KiB | 5 Q