I’ve been attempting for the past 4 hours to create a regex to get the information below and add it all to an array that i can run a forloop on. In about 2 hours, if this isn’t working, 304 people wont be getting a text message displaying that our school system now has a cancellation.
http://www.wane.com/generic/weather/closings/School_Delays_and_Closings
<tr class="B">
<td width="35%">Blackhawk Christian School</td>
<td width="25%">Allen</td>
<td width="80%">2 Hour Delay </td>
</tr>
<tr class="S">
<td width="35%">Southwest Allen County Schools</td>
<td width="25%">Allen</td>
<td width="80%">2 Hour Delay </td>
</tr>
What I need is a foreach td width="35%" add it to an array with the information of the school system, and the td wdith="80%" information. Because I don’t need this for just one school system, I need to check all of them in the list and display it to the user.
I’m doing:
$wanetv = get_url_contents("http://www.wane.com/generic/weather/closings/School_Delays_and_Closings");
To grab the webpage.
EDIT:
Tried to convert some C# posted below into PHP… can’t quite figure it out. Here’s my attempt:
$a = "<tr class='B'> <td width='35%'>Blackhawk Christian School</td> <td width='25%'>Allen</td> <td width='80%'>2 Hour Delay </td> </tr> <tr class='S'> <td width='35%'>Southwest Allen County Schools</td><td width='25%'>Allen</td><td width='80%'>2 Hour Delay </td> </tr> ";
$SchoolNameKeyword = "<td width='35%'>";
$DelayKeyword = "<td width='80%'>";
while (strlen(strstr($a, $SchoolNameKeyword))>0)
{
$a = substr($a,strrpos($a, $SchoolNameKeyword)+strlen($SchoolNameKeyword));
$schoolName = substr($a, 0,strrpos( $a, "<"));
$a = substr($a,strrpos($a, $DelayKeyword) + strlen($DelayKeyword));
$delay = substr( $a, 0,strrpos( $a, "<"));
$arr[$schoolName] = $delay;
}
print_r($arr);
Prints out:
Array
(
[Southwest Allen County SchoolsAllen2 Hour Delay ] => 2 Hour Delay
)
Such an example, using PHP’s DOM might look something like the following example. However, I would take exception to Andrew’s comments about HTML parsing being "somewhat more likely to stay working" as changes in the source HTML may affect it just as much as any regular expression.
The above uses classes/techniques that you are probably not familiar with. Do ask questions.