DOM

Learn how to select nodes with CSS selector in PHP, convert DOM into Array and Array into DOM.

Load HTML

Loads an HTML document from a string:

use Inphinit\DOM\Document; $handle = new Document(Document::HTML); $handle->load('<html><head></head><body><div x=\'abc"def\'>Hello World!</div><div id=\'foo\'>bar</div></body></html>'); var_dump($handle->document()); var_dump($handle->root());

Load XML

Loads an XML document from a string:

use Inphinit\DOM\Document; $handle = new Document(Document::XML); $handle->load('<root xmlns:book="https://book.io"><node foo="bar" baz="foobar">contents</node><book:tag>baz</book:tag></root>'); var_dump($handle->document()); var_dump($handle->root());

Load files

Load XML and HTML files

use Inphinit\DOM\Document; // parse XML $handle = new Document(Document::XML); $handle->load('sample.xml', true); // parse HTML $handle = new Document(Document::HTML); $handle->load('sample.html', true);

It is possible to load remote documents, but it is important to take some precautions and you need to define the context of the flows for the next appropriate load, according to the documentation: https://www.php.net/manual/en/function.libxml-set-streams-context.php.

use Inphinit\DOM\Document; $opts = array( 'http' => array( 'user_agent' => 'PHP libxml agent', ) ); $context = stream_context_create($opts); libxml_set_streams_context($context); // parse HTML $handle = new Document(Document::HTML); $handle->load('sample.html', true);

Array with DOM

{placeholder} DOM with Arrays.

DOM selector with

{placeholder} CSS Selector in PHP.

Go to homepage
Star us on Github