PHP: Validate XML String While Suppressing Errors

Here you go…

libxml_use_internal_errors(TRUE);
$foo = @simplexml_load_string($bar);
if ($foo === FALSE) {
	//Invalid XML
} else {
	//$bar is XML
}

If you want to display/log the errors…

foreach (libxml_get_errors() as $error) {
	//do something with $error->message
}
libxml_clear_errors();

Cheers 😀

Leave a Reply

Your email address will not be published. Required fields are marked *