简体   繁体   中英

Count played matchdays in html-table via simple html dom

I want to find the amount of days overall, the amount of days already played of a schedule.htm-file using simple html dom.

The.htm-file looks like this:

<tr><th>November 1 2020</th></tr>
<tr><th>visitor</th><th>score</th><th>home</th><th>score</th></tr>
<tr><td><a href="Wizards.htm">Wizards</a></td><td>120</td><td><a href="Hawks.htm">Hawks</a></td>111<td></td></tr>
<tr><td><a href="Celtics.htm">Celtics</a></td><td>98</td><td><a href="Heat.htm">Heat</a></td><td>108</td></tr>

<tr><th>November 2 2020</th></tr>
<tr><th>visitor</th><th>score</th><th>home</th><th>score</th></tr>
<tr><td><a href="Nets.htm">Nets</a></td><td>95</td><td><a href="Cavaliers.htm">Cavaliers</a></td>98<td></td></tr>
<tr><td><a href="Knicks.htm">Knicks</a></td><td>110</td><td><a href="Hornets.htm">Hornets</a></td><td>100</td></tr>

<tr><th>November 3 2020</th></tr>
<tr><th>visitor</th><th>score</th><th>home</th><th>score</th></tr>
<tr><td><a href="Thunder.htm">Thunder</a></td><td></td><td><a href="Timberwolves.htm">Timberwolves</a></td><td></td></tr>
<tr><td><a href="Trailblazers.htm">Trailblazers</a></td><td></td><td><a href="Lakers.htm">Lakers
... and so on... 

Please note, that the amount of games could vary each day. In the example above the overall amount would be = 3, played = 2, unplayed = 1.

Thus far I've come up with this:

$days = 0;
$days_played = 0;
$days_unplayed = $days - $days_played;

// find every row 
foreach($table->find('tr') as $row) { 

    // if row contains month then add a match-day
    if (strpos($row->find('th', 0)), 'November') OR
        strpos($row->find('th', 0)), 'December') OR
        strpos($row->find('th', 0)), 'January') OR
        strpos($row->find('th', 0)), 'February') OR
        strpos($row->find('th', 0)), 'March') OR
        strpos($row->find('th', 0)), 'April') OR
        strpos($row->find('th', 0)), 'May') !== false) {
            $days = $days + 1;
}

I understand this isn't the most beautiful solution, but it works so far.

My problem is, that I don't know how to get the days, which contain played / scored games. The games itself I can find with this...

$row->find('td', 1)

... since the second column of a row with tds always contains a score. But I need to count only one of them foreach matchday, otherwise I would come up with the amount of played games and not the match-days.

Since I'm stuck here, I would really appreciate an input. Thanks in advance.

If you want to check for the month and the day, one option could be to use a DateTime using DateTime::createFromFormat and check for the month name and day name.

This page has an overview of the different formats

$months = ["November", "December"];
foreach($table->find('tr') as $row) {    
    $res = $row->find('th', 0);
    if ($res && $counter%4 ===  0) {
        $dateTime = DateTime::createFromFormat("F d Y", $res->innertext());
        $month = $dateTime->format("F");
        $day = $dateTime->format("l");

        if (in_array($month, $months) && $day === "Sunday") { // I don't know the play day
            $days = $days + 1; 
        }

    }
    $counter++;    
}

var_dump($days);

Output

1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM