11-06-2008, 01:58 AM
|
#6 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
As much as I love regular expressions, I think that using the DOM extension is more suited to screen scraping in general. For the HTML snippet that buildakicker provided, perhaps something along the lines of the following might be useful.
PHP Code:
<?php
// Always use error reporting when developing
error_reporting(E_ALL | E_STRICT);
// Load in HTML string
$html = file_get_contents('snow.txt', false);
// Create new DOM document and load in our HTML fragment
$dom = new DOMDocument;
$dom->loadHTML($html);
// Create a new XPath handler for our HTML
$xpath = new DOMXPath($dom);
// Find the first <tr class="alternateRow"> element
$block = $xpath->query('//tr[@class="alternateRow"][1]')->item(0);
// Find the <td> elements inside our <tr>
$tds = $block->getElementsByTagName('td');
// Create array holding our required snowfall data
$result = array
(
'date' => trim($tds->item(0)->nodeValue),
'24hrs' => array
(
'6200' => trim($tds->item(1)->nodeValue),
'8200' => trim($tds->item(2)->nodeValue)
),
'totals' => array
(
'6200' => trim($tds->item(3)->nodeValue),
'8200' => trim($tds->item(4)->nodeValue)
)
);
// Tell them something interesting!
printf('Snowfall for %s at 6200 feet was %s.', $result['date'], $result['24hrs']['6200']);
|
|
|
|