2013-05-15 58 views
0

我使用以下方法來從一個站點刮足球賽事......剪出日期和分組日期?

// 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格式,因此可能比上面使用的方式更容易和更好地切出。

感謝您的任何建議。

+2

如果結果已經下令,除非你沒有迴音已是'$ Date'(所以店裏迴盪日期的地方)回聲'$ Date'。如果結果沒有排序,可以像'$ results [$ Date] [] = $ restofyourstring'一樣構建一個數組,並在最後循環。 – Wrikken

+0

@Wrikken結果按時間順序排列,但我不知道如何讓我的代碼知道多個日期是否相同,然後將它們組合在一起。明白了嗎? – Cully

回答

1

這應做到:

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

完美地工作。謝謝! – Cully