PHP code example of weijihao / php-htmldom

1. Go to this page and download the library: Download weijihao/php-htmldom 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/ */

    

weijihao / php-htmldom example snippets


use Weijihao\HtmlDom\HtmlDom;

...
$htmlDom = new Weijihao\HtmlDom\HtmlDom();

$html = $htmlDom->str_get_html( $str );
//or
$html = $htmlDom->file_get_html( $file_name );

$elems = $html->find($elem_name);
...



$htmlDom = new Weijihao\HtmlDom\HtmlDom();

$html = $htmlDom->str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

$html->find('div', 1)->class = 'bar';

echo $html->find('div[id=hello]', 0)->innertext;

echo "\n <br/>";

$html->find('div[id=hello]', 0)->innertext = 'foo';

echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
$htmlDom->dump_html_tree($html);

//

$htmlDom = new Weijihao\HtmlDom\HtmlDom();

$htmDom = $htmlDom->file_get_html("http://www.baidu.com");

foreach ($htmDom->find('a') as $element) {
    echo $element->href . $element->innertext . '<br>';
}
bash
composer