Closed
Description
Description
The following code:
<?php
$html = <<<HTML
<!DOCTYPE HTML>
<html>
<body>
<div></div>
</body>
</html>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$divs = iterator_to_array($dom->getElementsByTagName('div')->getIterator());
foreach ($divs as $div) {
$fragment = $dom->createDocumentFragment();
$fragment->appendXML('<p>Hi!</p>');
$div->replaceWith($fragment);
}
echo $dom->saveHTML();
Resulted in this output:
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
But I expected this output instead:
<!DOCTYPE HTML>
<html>
<body>
<p>Hi!</p>
</body>
</html>
It works as expected if I replace the line $div->replaceWith($fragment);
with $div->replaceWith(...$fragment->childNodes);
.
PHP Version
PHP 8.2.8
Operating System
Windows 10 x64.