I have an extremely simple implementation that pulls in a test bit of XML and attempts to validate it using DOMDocument. In testing, it's able to get through the LoadHTML() call fine, but as soon as I try and run validate(), the browser hangs forever and doesn't load. Here's the code:
$content = '<?xml version="1.0" encoding="utf-8"?><mainElement></mainElement>';
$dom = new DOMDocument;
$dom->LoadHTML($content);
if (!$dom->validate()) {
echo 'fail';
} else {
echo 'success!';
}
It seems that if you want to validate content loaded with loadHTML
, you need DOCTYPE
declaration (without it, you get an infinitive loop). For example, following code works and prints fail
$content = "
<!DOCTYPE html>
<html>
<body>
Content of the document......
</body>
</html>
";
$dom = new DOMDocument();
$dom->loadHTML($content);
if (!$dom->validate()) {
echo 'fail';
} else {
echo 'success!';
}
For XML it's more tolerant (it works even you didn't declare dtd
but it returns false). In your case, you might use loadXML
method and your code will print fail
.
Tested with php 7.0.13
.