loadHTML(file_get_contents("http://www.gov.je/weather/Pages/Jersey-Forecast.aspx")); $divs = $dom->getElementsByTagName("div"); $day = 0; $maxcount = 0; // How many max temperatures we have encountered (minus one for each min temp) $nightmode = 0; $valid = array(); $temp = array(); $misc = array(); $wind = array(); foreach ($divs as $div) { if ($day > 2) { break; } foreach ($div->attributes as $attr) { if ($attr->nodeValue == "forecastValid") { array_push($valid, $div->nodeValue); $day++; } if ($attr->nodeValue == "forecastText" || $attr->nodeValue == "forecastText last") { array_push($misc, $div->nodeValue); } if ($attr->nodeValue == "main") { $spans = $div->getElementsByTagName("span"); foreach ($spans as $span) { // If we get a min without a max, we're probably in night mode (only min and current listed) if ($span->nodeValue == "Max") { $maxcount++; } else if ($span->nodeValue == "Min" && $maxcount < 1) { $nightmode = 1; } else if ($span->nodeValue == "Min") { $maxcount--; } foreach ($span->attributes as $sattr) { if ($sattr->nodeValue == "temp" || $sattr->nodeValue == "currentc") { array_push($temp, $span->nodeValue); } } } } if ($attr->nodeValue == "wind") { $spans = $div->getElementsByTagName("span"); foreach ($spans as $span) { array_push($wind, $span->nodeValue); } } } } // Replace newlines with spaces, and strip multiple spaces for ($i = 0; $i < count($misc); $i++) { $misc[$i] = preg_replace("/[\r\n]+/", " ", $misc[$i]); $misc[$i] = preg_replace("/\s+/", " ", $misc[$i]); } for ($i = 0; $i < count($temp); $i++) { // $temp[$i] = str_replace("°", "°", $temp[$i]); } if (!$nightmode) { echo "
Forecasts | Graphs
\n\n"; echo "$valid[0]
\n";
if (!$nightmode) {
echo "Max: $temp[0], Min: $temp[1], Current: $temp[2], Wind: $wind[0]
\n";
} else {
echo "Min: $temp[0], Cur: $temp[1]
\n";
}
echo "Summary: $misc[0]
\n";
echo "Wind: $misc[1]
\n";
echo "
$valid[1]
\n";
if (!$nightmode) {
echo "Max: $temp[3], Min: $temp[4], Wind: $wind[2]
\n";
} else {
echo "Max: $temp[2], Min: $temp[3]
\n";
}
echo "Summary: $misc[2]
\n";
echo "Wind: $misc[3]