simplexml_load_string & libxml_get_errors functions
<?php
function isXml(string $value): bool
{
$prev = libxml_use_internal_errors(true);
$doc = simplexml_load_string($value);
$errors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($prev);
return false !== $doc && empty($errors);
}
echo isXml('<persons><person><name>John</name></person></persons>') ? 'Yes' : 'No';
echo isXml('<value>1</value><value>2</value>') ? 'Yes' : 'No';
Leave a Comment
Cancel reply