PHP code example of zenthangplus / html-dom-parser

1. Go to this page and download the library: Download zenthangplus/html-dom-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zenthangplus / html-dom-parser example snippets



$dom = \HTMLDomParser\DomFactory::load('<div class="container"><div class="anchor"><a href="#">Test</a></div></div>');
$a = $dom->findOne('.container a');
echo $a->text();
// Output: Test


$dom = \HTMLDomParser\DomFactory::load('<div>Test</div>');


$dom = \HTMLDomParser\DomFactory::loadFile('document.html');


$node = \HTMLDomParser\NodeFactory::load('<div><a href="#">Test</a></div>');


$node = \HTMLDomParser\NodeFactory::loadFile('document.html');


$dom = \HTMLDomParser\DomFactory::loadFile('document.html');
$dom->find('div');
$dom->find('#container');
$dom->find('#container .content ul>li a.external-link');
$dom->find('#container .content ul>li a[data-id=link-1]');


$dom = \HTMLDomParser\DomFactory::loadFile('document.html');
$node = $dom->findOne('#container .content ul>li');
$anchorNode = $node->findOne('a.external-link');

// Even traverse in a separate Node
$node = \HTMLDomParser\NodeFactory::load('<ul class="list"><li>Item 1</li><li>Item 2</li></ul>');
$node->find('ul.list li');