Is there some way to differentiate XML from HTML with PHP DomDocument?
I looked in the docs and didn't find anything.
I'm looking for a function like check($string) that returns 'is XML' or 'is HTML' for each $string.
Is there some way to differentiate XML from HTML with PHP DomDocument?
I looked in the docs and didn't find anything.
I'm looking for a function like check($string) that returns 'is XML' or 'is HTML' for each $string.
There is no such function, but you can rest assured that some $string is well-formed XML when DOMDocument::loadXML() returned true (set recover to false). A HTML document fails with that.
For HTML you can use DOMDocument::loadHTML() to check if a document can be loaded as HTML. HTML is not as strict as XML.
Use preg_match extension. Example:
if( preg_match('/<html[^>]*>/', $string) ) {
{
// ... actions for XML ...
} elseif( preg_match('/<\?xml[^?]*\?>/', $string) ) {
// ... actions for HTML ...
} else {
// ... actions for another ...
}