PHP code example of eyupcanakman / simple-html-dom

1. Go to this page and download the library: Download eyupcanakman/simple-html-dom 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/ */

    

eyupcanakman / simple-html-dom example snippets


use eyupcanakman\SimpleHtmlDom\HtmlDomParser;

$dom = HtmlDomParser::str_get_html($str);
// or
$dom = HtmlDomParser::file_get_html($file);

$element = $dom->findOne('#css-selector');
$elements = $dom->findMulti('.css-selector');
$elementOrFalse = $dom->findOneOrFalse('#css-selector');
$elementOrNull = $dom->findOneOrNull('#css-selector');
$elementsOrFalse = $dom->findMultiOrFalse('.css-selector');

// PHP 8.0+ nullsafe operator
$text = $dom->findOneOrNull('.maybe-missing')?->text();

// Find text nodes inside an element
$textNodes = $dom->find('div text');        // all descendant text nodes
$directText = $dom->find('p > text');       // direct child text nodes only
$comments = $dom->find('div comment');      // comment nodes inside div