";
}
return $offset;
}
function show_array($array, $level, $sub){
if (is_array($array) == 1){ // check if input is an array
foreach($array as $key_val => $value) {
$offset = "";
if (is_array($value) == 1){ // array is multidimensional
echo "
";
$offset = do_offset($level);
show_array($value, $level+1, 1);
}
else{ // (sub)array is not multidim
if ($sub != 1){ // first entry for subarray
// echo "
";
$offset = do_offset($level);
}
$sub = 0;
echo $offset . "" . $key_val .
" | " . $value . " | ";
// echo "
";
}
} //foreach $array
}
else{ // argument $array is not an array
return;
}
}
function html_show_array($array){
echo "\n";
show_array($array, 1, 0);
echo "
\n";
}
//end functions
//---------------------------------------------------------------
//set current game week
$Current_Week = 'p1'; #preweek 3, just to see if the script works. When the season starts, '1' will denote week one etc.
//load source code, depending on the current week, of the website into a variable as a string
$url = "http://sports.yahoo.com/nfl/scoreboard?w=$Current_Week";
$string = file_get_contents($url);
//set search pattern (using regular expressions)
$find = '|(.*?).*?.*?(.*?) |is';
//search the string for the pattern, and store the content found inside the set of parens in the array $matches
//$matches[1] is going to hold team names in the order they appear on the page, and $matches[2] the scores
preg_match_all($find, $string, $matches);
//initiate scores array, to group teams and scores together in games
$scores = array();
//count number of teams found, to be used in the loop below
$count = count($matches[1]);
//loop from 0 to $count, in steps of 2
//this is done in order to group 2 teams and 2 scores together in games, with each iteration of the loop
//trim() is used to trim away any whitespace surrounding the team names and scores
//strip_tags() is used to remove the HTML bold tag () from the winning scores
for ($i = 0; $i < $count; $i += 2) {
$away_team = trim($matches[1][$i]);
$away_score = trim($matches[2][$i]);
$home_team = trim($matches[1][$i + 1]);
$home_score = trim($matches[2][$i + 1]);
$winner = (strpos($away_score, '<') === false) ? $home_team : $away_team;
$scores[] = array(
'awayteam' => $away_team,
'awayscore' => strip_tags($away_score),
'hometeam' => $home_team,
'homescore' => strip_tags($home_score),
'winner' => $winner
);
}
echo "
";
echo "Scores from week: $Current_Week";
echo " ";
echo " ";
//see how the scores array looks
// echo '' . print_r($scores, true) . ' ';
$input = $scores;
$cols = 5;
// echo " ";
// echo count($scores);
//echo " ";
// echo "";
// echo "";
// echo "away_team | ";
// echo "away_score | ";
// echo "home_team | ";
// echo "home_score | ";
// echo "winner | ";
// echo " ";
// for ($i=0; $i < count($input); $i++)
// {
// echo "";
// for ($c=0; $c<$cols; $c++)
// {
// echo "$input[$i] | ";
// echo "$away_team | ";
// echo "$away_score | ";
// echo "$home_team | ";
// echo "$home_score | ";
// echo "$winner | ";
// }
// echo " ";
// }
// echo " ";
// foreach($scores as $key_val => $value) {
// $offset = "";
// if (is_array($value) == 1){ // array is multidimensional
// echo "";
// $offset = do_offset($level);
// echo $offset . "" . $key_val . " | ";
// show_array($value, $level+1, 1);
// }
// else{ // (sub)array is not multidim
// if ($sub != 1){ // first entry for subarray
// echo " ";
// $offset = do_offset($level);
// }
// $sub = 0;
// echo $offset . "" . $key_val .
// " | " . $value . " | ";
// echo " \n";
// }
// } //foreach $array
//game results and winning teams can now be accessed from the scores array
//e.g. $scores[0]['awayteam'] contains the name of the away team (['awayteam'] part) from the first game on the page ([0] part)
html_show_array($scores);
?>
|