- var_dump: documentation ( source)
- libxml_get_errors: documentation ( source)
- libxml_clear_errors: documentation ( source)
- libxml_use_internal_errors: documentation ( source)
<?php
/**
* Checks if a XML file's structure is valid.
* @author Julius Beckmann <php@h4cc.de>
* @param $file string
* @return bool
*/
function isXmlStructureValid($file) {
$prev = libxml_use_internal_errors(true);
$ret = true;
try {
new SimpleXMLElement($file, 0, true);
} catch(Exception $e) {
$ret = false;
}
if(count(libxml_get_errors()) > 0) {
// There has been XML errors
$ret = false;
}
// Tidy up.
libxml_clear_errors();
libxml_use_internal_errors($prev);
return $ret;
}
$xml = '<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx</center>
</body>
</html>';
var_dump(isXmlStructureValid($xml));