I am having issues removing a node using PHP DomDocument.
I have some HTML like so:
<!DOCTYPE HTML "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test</title>
<script id="fr21" type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
</body>
</html>
I attempt to remove the script node like so:
$jquery_node = $doc->getElementById('fr21');
$head_node = $jquery_node->parentNode;
$head_node->removeChild($jquery_node);
I then try to view the HTML by echo:
echo $doc->saveHTML().'<br><br>';
The HTML then becomes this:
<!DOCTYPE HTML>
<html>
<body><p>-//W3C//DTD HTML 4.0 Transitional//EN"></p>
<body>
</body>
</html>
What just happened? The HTML has been mangled? Am I not removing the node correctly?
The weird thing is when I calculate the xPath for the jquery node it is shown as if its attached to the body node rather than the head node?
/html[1]/body[1]/script[1]
If you look at the errors, you will see that it says:
Change the DOCTYPE to read
and it will work as expected: demo