Search code examples
phpxpathdomxpath

How to get cell by class?


I'm trying to iterate over a table which have the following composition:

<table><thead><tr class="sub-head"></tr></thead><tbody><tr class="group-head expanded live" id="date_matches-109-45564" stage-value="1"><th colspan="5"><h3><span class="flag_16 left_16 japan_16_left">Japan - J1 League</span></h3></th><th class="competition-link"> <a href="/national/japan/j1-league/2018/regular-season/r45564/"><span>More…</span></a></th></tr><tr class="even highlight expanded first last  match no-date-repetition" data-timestamp="1526803200" id="block_home_matches_30_match-2722312" data-competition="109"><td class="minute visible">89'</td><td class="team team-a "><a href="/teams/japan/vissel-kobe/1328/" title="Vissel Kobe">Vissel Kobe</a></td><td class="score-time score"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/?ICID=HP_MS_01_01">4 - 0</a></td><td class="team team-b "><a href="/teams/japan/consadole-sapporo/1347/" title="Consadole Sapporo">Consadole Sapporo</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/" title="More info">More info</a></td></tr><tr class="group-head live expanded loaded" id="date_matches-110-45567" stage-value="2"><th colspan="5"><h3><span class="flag_16 left_16 japan_16_left">Japan - J2 League</span></h3></th><th class="competition-link"> <a href="/national/japan/j2-league/2018/regular-season/r45567/"><span>More…</span></a></th></tr>          <tr class="even highlight expanded first last  match no-date-repetition" data-timestamp="1526803200" id="block_home_matches_30_match-2721813" data-competition="110"><td class="minute visible">90'</td><td class="team team-a "><a href="/teams/japan/ehime-fc/6521/" title="Ehime">Ehime</a></td><td class="score-time score"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/?ICID=HP_MS_01_01">1 - 2</a></td><td class="team team-b "><a href="/teams/japan/kyoto-sanga/1340/" title="Kyoto Sanga">Kyoto Sanga</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/" title="More info">More info</a></td></tr>
<tr class="group-head expanded live" id="date_matches-1110-45216" stage-value="3"><th colspan="5"><h3><span class="flag_16 left_16 korea-republic_16_left">Korea Republic - K League 2</span></h3></th><th class="competition-link"> <a href="/national/korea-republic/k-league/2018/regular-season/r45216/"><span>More…</span></a></th></tr><tr class="even highlight expanded first last  match no-date-repetition" data-timestamp="1526806800" id="block_home_matches_30_match-2725079" data-competition="1110"><td class="minute visible">HT</td><td class="team team-a "><a href="/teams/korea-republic/anyang/2989/" title="Anyang">Anyang</a></td><td class="score-time score"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/?ICID=HP_MS_03_01">0 - 1</a></td><td class="team team-b "><a href="/teams/korea-republic/gwangju-fc/18351/" title="Gwangju">Gwangju</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/" title="More info">More info</a></td></tr><tr class="ad"><td colspan="6"><div class="block_ad ad_date_matches block clearfix"></div></td></tr></tbody></table>

I loaded the html inside a DOMDocument in this way:

$dom = new DOMDocument();
$dom->loadHTML("string which contains the table");

So I get all the tr that contains the class match, there are a total of 3 tr:

$xpath = new DOMXPath($dom);
$rows = $xpath->query("//tr[contains(@class, 'match')]");

started the foreach:

foreach($rows as $tr)
{
    $c = $tr->find('td.minute');
    echo $c;
}

I tried to access to the cell which contains the class minute but I got this error:

Call to undefined method DOMElement::find()

What I did wrong?


Solution

  • $rows = $xpath->query("//tr[contains(@class, 'match')]");
    
    foreach($rows as $row) {
        $td = $xpath->query("./td[contains(@class, 'minute')]", $row);
        echo $td->item(0)->textContent . "<br>";
    }