减少刮刮日期和按日分组?

时间:2013-05-15 00:46:57

标签: php loops simple-html-dom

我使用以下方法从网站上刮下足球场......

// Retrieve the URL
$url = 'http://fantasy.mlssoccer.com/fixtures/';
$html = @file_get_html($url);

//cuts out just the table
$FullFixTable = $html->find('table[class=ismFixtureTable]',0);

// Find all results and break them up into pieces
foreach($FullFixTable->find('tr[class=ismFixture]') as $ResultsToCutOut) 
{
    $GameDate = $ResultsToCutOut->find('td',0)->innertext;
      $Date = substr($GameDate, 0, 6);
      $Time = substr(substr($GameDate, 0, -4), (strlen(substr($GameDate, 0, -6))-5)*-1);
    $HomeTeam = $ResultsToCutOut->find('td',1)->innertext;
    $AwayTeam = $ResultsToCutOut->find('td',5)->innertext;

    echo $Date.' '.$Time.' '.$HomeTeam.' v '.$AwayTeam.'<br>';
}

这回应......

May 15 7:30PM Philadelphia Union v Los Angeles Galaxy
May 18 5:00PM Toronto FC v Columbus Crew
May 18 7:00PM Vancouver Whitecaps v Portland Timbers
May 18 7:30PM Philadelphia Union v Chicago Fire
May 18 8:30PM Houston Dynamo v New England Revolution
May 18 10:30PM San Jose Earthquakes v Colorado Rapids
May 18 10:30PM Seattle Sounders FC v FC Dallas
May 19 1:00PM New York Red Bulls v Los Angeles Galaxy
May 19 5:00PM D.C. United v Sporting Kansas City
May 19 10:30PM Chivas USA v Real Salt Lake

哪个好......但我想按此顺序提供信息......

May 15
  7:30PM Philadelphia Union v Los Angeles Galaxy
May 18
  5:00PM Toronto FC v Columbus Crew
  7:00PM Vancouver Whitecaps v Portland Timbers
  7:30PM Philadelphia Union v Chicago Fire
  8:30PM Houston Dynamo v New England Revolution
  10:30PM San Jose Earthquakes v Colorado Rapids
  10:30PM Seattle Sounders FC v FC Dallas
May 19
  1:00PM New York Red Bulls v Los Angeles Galaxy
  5:00PM D.C. United v Sporting Kansas City
  10:30PM Chivas USA v Real Salt Lake

知道我会怎么做吗?我知道我可以使用substr来删除内容,但我不知道如何按类似日期对其进行分组。日期总是M d g:iA T格式,因此可能比我上面使用的方式更容易,更好的方法。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

这应该这样做:

if ( !isset($lastDate) || $Date !== $lastDate ) {
    echo $Date.'<br>';
    echo $Time.' - '.$HomeTeam.' v '.$AwayTeam.'<br>';
    $lastDate = $Date;
} else {
    echo $Time.' - '.$HomeTeam.' v '.$AwayTeam.'<br>';
}