Search code examples
phpsimple-html-dom

Wrong parsing syntax for PHP Simple Dom HTML Parser


I am using this code:

$results = '<div class="collection__list" data-type="Other1">DIV1</div><div class="collection__list" data-type="Other2">DIV2</div>'; 

$html = str_get_html($results);

$articles = $html->find('div[class=collection__list, data-type=Other1]', 0);

echo $articles; 

And nothing prints out. Isn't it suppose to print out the "DIV1" div?


Solution

  • You can't do search for multiple attributes with Simple HTML DOM Parser. Perhaps it's too simple? If you use the built-in DOM parser, this works fine, though it does require some familiarity with XPath:

    <?php
    $results = '<div class="collection__list" data-type="Other1">DIV1</div><div class="collection__list" data-type="Other2">DIV2</div>'; 
    $dom = new DomDocument();
    $dom->loadHTML($results, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
    $xpath = new DomXpath($dom);
    $nodes = $xpath->query("//div[@class='collection__list' and @data-type='Other1']/text()");
    foreach ($nodes as $node) {
        echo $node->nodeValue;
    }
    

    I suppose one could loop over the search, but really using the built-in functions is going to be a more robust and standardized solution, or at least use a modern solution. This "library" is really a relic from the past and doesn't conform to any modern programming practices.

    <?php
    $results = '<div class="collection__list" data-type="Other1">DIV1</div><div class="collection__list" data-type="Other2">DIV2</div>'; 
    $html = str_get_html($results);
    foreach ($html->find('div[class=collection__list]') as $article) {
        if ($article->attr["data-type"] === "Other1") {
            echo $article;
            break;
        }
    }