简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM