James
James

Reputation: 1769

Differentiating HTML and XML with PHP DomDocument

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.

These similar questions here in SO didn't help me.

Upvotes: 4

Views: 511

Answers (2)

hakre
hakre

Reputation: 198204

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.

Upvotes: 2

Quazer
Quazer

Reputation: 383

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 ...
}

Upvotes: 0

Related Questions