Created
January 2, 2014 20:45
-
-
Save failpunk/8226492 to your computer and use it in GitHub Desktop.
This script gives you the current MLB draft position of the Chicago Cubs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CubsDraft | |
{ | |
public $_stats = []; | |
protected $_table = null; | |
public function __construct() | |
{ | |
$this->_table = $this->extractTable(); | |
$this->_stats = $this->buildStats(); | |
} | |
public function cubsPick() | |
{ | |
$count = 1; | |
$stats = array_reverse($this->_stats); | |
foreach($stats as $team => $stat) | |
{ | |
if($team == 'Chicago Cubs') | |
return $count; | |
else | |
$count++; | |
} | |
} | |
protected function extractTable() | |
{ | |
$page = file_get_contents('http://espn.go.com/mlb/standings/_/group/9'); | |
$table_begin = strpos($page, '<div', strpos($page, 'id="my-teams-table"')); | |
$page = substr($page, $table_begin); | |
$page = substr($page, 0, stripos($page, '<!-- end Standings Table -->') - 1); | |
$dom = new DOMDocument; | |
$dom->loadHTML($page); | |
return $dom->getElementsByTagName('table'); | |
} | |
protected function buildStats() | |
{ | |
$stats = []; | |
$rows = $this->_table->item(0)->getElementsByTagName('tr'); | |
// loop over the table rows | |
foreach($rows as $row) | |
{ | |
$team = null; | |
$gameBack = null; | |
// get each column by tag name | |
foreach($row->getElementsByTagName('td') as $pos => $item) | |
{ | |
if($item->nodeValue == 'Major League Baseball' || $item->nodeValue == 'MLB' || $item->nodeValue == null) | |
continue; | |
if($pos == 0) | |
$team = $item->nodeValue; | |
if($pos == 4) | |
$gameBack = $item->nodeValue; | |
} | |
if(!$team) | |
continue; | |
$stats[$team] = $gameBack; | |
} | |
return $stats; | |
} | |
} | |
$draftPicks = new CubsDraft(); | |
var_dump('The Chicago Cubs currently have the number ' . $draftPicks->cubsPick() . ' overall pick.'); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment