如何逐类获取细胞?

时间:2018-05-20 09:53:42

标签: php xpath domxpath

我试图迭代具有以下组成的表格:

<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>

我以这种方式将html加载到DOMDocument内:

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

所以我得到了包含类match的所有tr,总共有3 tr:

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

开始了foreach:

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

我尝试访问包含类cell的{​​{1}}但我收到此错误:

  

调用未定义的方法DOMElement :: find()

我做错了什么?

2 个答案:

答案 0 :(得分:1)

以下是如何使用$tr分钟找到所有单元格(来自给定的class):

$cells = $xpath->query("./td[contains(@class, 'minute')]", $tr);

答案 1 :(得分:1)

Album