Search code examples
phphtmlsimple-html-dom

PHP Simple HTML DOM Parser : Select multiple items


Here is my problem, i want to get text from HTML using HTML DOM.

<div class="smalldesc">
    <div itemprop="datePublished" class="date">Kamis, 25 Mei 2017 | 14:49 WIB</div>
    <div itemprop="author" itemscope itemtype="http://schema.org/Person" class="author">Oleh : <b></b><b>...</b></div>
</div>

The text i want to get is "Kamis, 25 Mei 2017 | 14:49 WIB". Here what i tried :

$data->find('div[itemprop=datePublished class=date]',0)

and

$data->find('div[itemprop="datePublished" class="date"]',0)

But i still get null, how do i select two items at the same time?


Solution

  • Simple HTML DOM Parser doesn't provide a feature to select an element with identifying two or more attributes. Saying that, div[itemprop=datePublished class=date] is seen a garbage to selector class.

    I don't recommend this library at all either while there is built-in, more performative and easy way in parsing DOM: DOMXPath.

    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $dox = new DOMXPath($dom);
    $dateEl = $dox->query("//div[@itemprop='datePublished'][@class='date']")->item(0);
    
    echo $dateEl->textContent; // Kamis, 25 Mei 2017 | 14:49 WIB
    

    PHP live demo