webstar Posted October 12, 2007 Share Posted October 12, 2007 Ok I will first say I know very very very little php and this is confusing me. This script was made for me and worked until this year and I can't figure it out. It reads from this page. http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm and pretty much need to reads just the parts between. <!-- TemplateBeginEditable name="content" --> and </DIV> on the HTML below. Problem is this year it started doing this. Reading "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm". Searching for " "..." ". Searching for "". Number of current events = 1 Number of old events = 0 Fatal error: Cannot use string offset as an array in C:\WEATHERSCRIPTS\mto\test\roadclosed.php on line 44 And they changed the webpage and I can't figure out how to fix it. I've been trying to for 4 days now when I get time off work. I figure it is something really easy but not knowing PHP it isn't going to be easy for me to find. I've tried changing the part of the code but with no luck either. I hope someone can help. CHANGES I HAVE TRIED $content = find_data($webpage, "TemplateBeginEditable name=\"content\"", "</DIV>"); // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE BORDER", "</TABLE>"); // Look for the events HTML table PHP SCRIPT(with the emails removed) <?php // Declare variables // $debug = true; $url = "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; //$url = "./roadclosed1/rdclosure.htm"; #$email_recip = "email1"; $email_recip = "email2"; #$email_subject = MOVED TO LINE 47 $email_from = "WxServer Roads"; $email_from_addr = "Email3"; $message_header = "\n====START====\n"; $message_footer = "\n\n http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; $eventfilename = "roadclosed.txt"; // DON'T EDIT PAST HERE UNLESS YOU KNOW WHAT YOU'RE DOING // // MAIN PROGRAM /// $webpage = read_web($url); $content = find_data($webpage, "<DIV>", "</DIV>"); // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE BORDER", "</TABLE>"); // Look for the events HTML table $curr_events = read_html_table($content,true); // Parse the events HTML table $old_content = read_events_file(); // Read the old events HTML table if ($old_content) { $old_events = read_html_table($old_content, true); // Parse the old events HTML table } else { unset($old_events); } if ($debug) { print("Number of current events = " . count($curr_events) . "\n"); } if ($debug) { print("Number of old events = " . count($old_events) . "\n"); } //Go through the current events and look for a corrisponding old event $send_email = false; // Flag used to see if an email should be sent for new closure events $event_message = $message_header; for ($curr_event_num = 0; $curr_event_num < count($curr_events); $curr_event_num++) { $curr_event_found = false; for ($old_event_num = 0; $old_event_num < count($old_events); $old_event_num++) { if ($debug) { print("Comparing (" . $curr_events[$curr_event_num][0] . "?" . $old_events[$old_event_num][0] . ") & (" . $curr_events[$curr_event_num][1] . "?" . $old_events[$old_event_num][1] . ") & (" . $curr_events[$curr_event_num][2] . "?" . $old_events[$old_event_num][2] . ")\n"); } if (($curr_events[$curr_event_num][0] == $old_events[$old_event_num][0]) && ($curr_events[$curr_event_num][1] == $old_events[$old_event_num][1]) && ($curr_events[$curr_event_num][2] == $old_events[$old_event_num][2])) { // Found a matching event $curr_event_found = true; } } if (!$curr_event_found) { // Add the not found current event to the message $send_email = true; if ($debug) { print ("Found a new event: \nRegion: " . $curr_events[$curr_event_num][1] . " \nHighway(s): " . $curr_events[$curr_event_num][0] . " \nReason: " . $curr_events[$curr_event_num][2] . "\n=====END=====\n"); } // $event_message .= ">>> NEW <<<\n"; $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"; $email_subject .= " Hwy: " . $curr_events[$curr_event_num][0]; } else { // if ($debug) { print ("Found a previously sent event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"); } // $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"; } } $event_message .= $message_footer; //Send an email if there is a new event //Send an email if there is a new event if ($send_email and !preg_match('/there\s*are\s*no\s*reported\s*closures/i', $event_message)) { print ("Sending the message below to " . $email_recip . ", from \"" . $email_from . "\" <" .$email_from_addr . ">.\n" . $event_message . "\n"); mail ($email_recip, $email_subject, $event_message, "From: \"" . $email_from . "\" <" .$email_from_addr . ">"); print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n==========\n"); } //Save the events to check the next time the script is run write_events_file($content); // Functions // //read_web - Read the web page // $strURL ==> URL of the webpage function read_web($strURL) { global $debug; $buffer = ""; if($debug){ print("Reading \"$strURL\".\n"); } $fh = fopen($strURL, "rb"); if ($fh === false) { return ""; } while (!feof($fh)) { $buffer .= fread($fh, 1024); } fclose($fh); return $buffer; } // end function read_web // find)data - Gets a substring of the webpage by scraping the data // $strFile ==> text of the webpage // $strStart_Pattern ==> start of the substring // $strEnd_Pattern ==> end of the substring function find_data($strFile,$strStart_Pattern,$strEnd_Pattern, $intStart_Position = 0) { global $debug; if($debug){ print("Searching for \"$strStart_Pattern\"...\"$strEnd_Pattern\".\n"); } $first_match = strpos($strFile,$strStart_Pattern, $intStart_Position); if ($first_match) { # find the begining of the tag for ($i = $first_match; $i>0;$i--) { $char = $strFile[$i]; if ($char == "<" ) { $first_match = $i; //record the location of the tag break; } } $partialbuffer = substr($strFile,$first_match,strlen($strFile) - $first_match); # find the end of the sub string $second_match = strpos($partialbuffer,$strEnd_Pattern); return substr($partialbuffer,0,$second_match + strlen($strEnd_Pattern)); } else { return(false); } } //end function find_data // read_html_table - Read the contents of an HTML table and return the array(s) // strHTMLTable ==> HTML table text // boolSkipFirstRow ==> Skip the first row if it contains column titles (true/false) function read_html_table($strHTMLTable, $boolSkipFirstRow) { global $debug; $table = ""; $th = ""; $td = ""; if($boolSkipFirstRow) { $rownum = -2; } else { $rownum = -1; } $rowopen = false; $strPos = 0; do { // Scrape through the table if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TR") { //Look for the rows $strPos +=3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the row open tag $rowopen = true; $rownum++; } if (strtoupper(substr($strHTMLTable, $strPos, 5)) == "</TR>") { $rowopen = false; $strPos += 5; // jump to the end of the close tag } if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TH") { // Look for headers $strPos += 3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the header open tag $endPos = strpos(strtoupper($strHTMLTable), "</TH>", $strPos); // Find the header close tag $th[] = substr($strHTMLTable, $strPos, $endPos - $strPos); $strPos = $endPos + 5; if($debug) { print("Found column header \"" . end($th) . "\" (" . (count($th) - 1) . ")\n"); } } if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TD") { // Look for cells $strPos += 3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the cell open tag $endPos = strpos(strtoupper($strHTMLTable), "</TD>", $strPos); // Find the cell close tag $td[$rownum][] = str_replace(array("\n","\t")," ",substr($strHTMLTable, $strPos, $endPos - $strPos)); $td[$rownum][count($td[$rownum]) - 1] = str_replace("\r"," ",$td[$rownum][count($td[$rownum]) - 1]); $td[$rownum][count($td[$rownum]) - 1] = preg_replace("/[\s]+/"," ",$td[$rownum][count($td[$rownum]) - 1]); $strPos = $endPos + 5; if($debug) { print("Found cell data \"" . end($td[$rownum]) . "\" (" . $rownum . "|" . (count($td[$rownum]) - 1) . ")\n"); } } $strPos++; } while ($strPos <= strlen($strHTMLTable)); return $td; /* if ($boolRowOneColumnTitles) { if ($th[0] != "") { // Create a new array that uses the headers as the array key $newtd = ""; $header = ""; $row = ""; if ($debug) { print(count($th) . " headers\n"); } if ($debug) { print(count($td) . " rows\n"); } for ($row = 0; $row <= count($td) - 1; $row++) { for ($cell = 0; $cell <= count($td[$row]) - 1; $cell++) { if ($debug) { print("(". $row . "|" . $cell . ") => " . $th[$cell] . "(" . $row . ") = " . $td[$row][$cell] . "\n"); } $newtd[$th[$cell]][$cell] = $td[$row][$cell]; } } return $newtd; } else { // There are no headers, so just return the td array. return $td; } } else { // Just return what's in the td array return $td; } */ } //end function readhtmltable // read_events_file - Reads the old events from event file function read_events_file() { global $debug; $eventfile = "roadclosed.txt"; $buffer = ""; if (is_file($eventfile)) { // Check to see if the event log exists $fh = fopen($eventfile, "r"); while (!feof($fh)) { $buffer .= fread($fh, 1024); } return $buffer; } return; // Return an empty array } // end function read_events_file // write_events_file - Writes the current events to a file to be read later // - strEvents ==> array of events function write_events_file($strEvents) { global $debug; $eventfile = "roadclosed.txt"; $fh = fopen($eventfile, "w"); if ($fh === false) { if ($debug) { print("ERROR: Could not write to \"" . $eventfile . "\"!\n"); } } else { fwrite ($fh, $strEvents); fclose ($fh); } return; } // end function write_events_file print ("ClosedRoadScript"); print date(" (G:i:s)"); ?> HTML SOURCE OF WEBPAGE WITH AN ACTIVE ALERT <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "hmpro6.dtd"> <HTML LANG="en-ca"> <HEAD> <BASE HREF="http://www.mto.gov.on.ca/english/"> <LINK HREF="http://www.mto.gov.on.ca/css/style.css" REL="stylesheet" TYPE="text/css"> <META NAME="Author" CONTENT="Ontario Ministry of Transportation"> <!-- TemplateBeginEditable name="head" --> <META NAME="robots" CONTENT="noindex,nofollow"> <TITLE>Road Closures</TITLE> <!-- TemplateEndEditable --> </HEAD> <BODY BGCOLOR="#ffffff"> <P></P> <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0"> <TR> <TD><A HREF="http://www.gov.on.ca/MBS/english/index.html"><IMG SRC="images/ont_head.gif" WIDTH="246" HEIGHT="61" BORDER="0" ALT="Government of Ontario"></A><!-- TemplateBeginEditable name="skipMenu" --><A HREF="traveller/conditions/rdclosure.htm#skipmenu"><IMG SRC="images/bump.gif" HEIGHT="1" WIDTH="1" BORDER="0" ALT="| Skip Navigation Menu |"></A><!-- TemplateEndEditable --></TD> <TD ALIGN="right"><A HREF="index.html"><IMG SRC="images/mto_e.gif" WIDTH="259" HEIGHT="51" BORDER="0" ALT="Ministry of Transportation"></A><BR><A ACCESSKEY="1" HREF="http://www.gov.on.ca/MBS/english/index.html"><IMG SRC="images/central-site.gif" WIDTH="63" HEIGHT="10" BORDER="0" ALT="Government of Ontario Central Site."></A><!-- TemplateBeginEditable name="topFeedback" --><A ACCESSKEY="2" HREF="feedback/mtoinfo.htm"><IMG SRC="images/feedback.gif" WIDTH="54" HEIGHT="10" BORDER="0" ALT="Contact us for questions and comments."></A><!-- TemplateEndEditable --><A ACCESSKEY="3" HREF="search.htm"><IMG SRC="images/search.gif" WIDTH="43" HEIGHT="10" BORDER="0" ALT="Search the Ministry of Transportation Web site."></A><A ACCESSKEY="4" HREF="map.htm"><IMG SRC="images/site-map.gif" WIDTH="52" HEIGHT="10" BORDER="0" ALT="Site map for the ministry Web site."></A><!-- TemplateBeginEditable name="topFrench" --><A ACCESSKEY="5" HREF="/french/traveller/conditions/rdclosure.htm"><SPAN LANG="fr-ca"><IMG SRC="images/francais.gif" WIDTH="48" HEIGHT="10" BORDER="0" ALT="Version française de cette page."></SPAN></A><!-- TemplateEndEditable --></TD> </TR> <TR> <TD COLSPAN="2" STYLE="background-color: #666666"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="1" ALT=" "></TD> </TR> <TR> <TD COLSPAN="2"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="3" ALT=" "></TD> </TR> </TABLE> <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="0"> <TR> <TD CLASS="menuTop"><A HREF="index.html">Home</A></TD> <TD CLASS="menuTop"><A HREF="about/">About the Ministry</A></TD> <TD CLASS="menuTop"><A HREF="dandv/">Drivers & Vehicles</A></TD> <TD CLASS="menuTop"><A HREF="traveller/">Traveller's Information</A></TD> <TD CLASS="menuTop"><A HREF="trucks/">Trucks & Buses</A></TD> <TD CLASS="menuTop"><A HREF="safety/">Road Safety</A></TD> </TR> <TR> <TD CLASS="menuTop"><A HREF="new/">What's New</A></TD> <TD CLASS="menuTop"><A HREF="engineering/">Highways</A></TD> <TD CLASS="menuTop"><A HREF="business/">Opportunities</A></TD> <TD CLASS="menuTop"><A HREF="pubs/">Publications</A></TD> <TD CLASS="menuTop"><A HREF="sites/sites.htm">Related Links</A></TD> <TD CLASS="menuTop"><A HREF="faq/">FAQ</A></TD> </TR> <TR> <!-- TemplateBeginEditable name="location" --> <TD CLASS="location" COLSPAN="6">Location: <A HREF="index.html">Ministry Home</A> > <A HREF="traveller/">Traveller's Information</A> > <A HREF="traveller/conditions/">Road Conditions</A> > Road Closures</TD> <!-- TemplateEndEditable --> </TR> <TR> <TD COLSPAN="6"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="3" ALT=" "></TD> </TR> <TR> <TD COLSPAN="6" STYLE="background-color: #666666"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="1" ALT=" "></TD> </TR> </TABLE> <BR> <!-- TemplateBeginEditable name="content" --> <DIV CLASS="content"> <H1>Road Closures</H1> <P>Please press the Refresh or Reload button at the top of your screen to see the most recent updates.</P> <P><B>Road closures and unscheduled events</B></P> <P><B>Last updated: October 12th, 2007at 2:20 a.m.</B></P> </DIV> <DIV CLASS="content"> <TABLE BORDER="1" CELLPADDING="2" CELLSPACING="0"> <TR> <TH BGCOLOR="#FFFFE6" WIDTH="15%" HEIGHT="33">Highway</TH> <TH BGCOLOR="#FFFFE6" WIDTH="22%" HEIGHT="33">Area</TH> <TH BGCOLOR="#FFFFE6" WIDTH="61%" HEIGHT="33" ALIGN="LEFT">Reason for closure</TH> </TR> <TR> <TH BGCOLOR="#FFFFE6" WIDTH="15%" HEIGHT="19">401</TH> <TH BGCOLOR="#FFFFE6" WIDTH="22%" HEIGHT="19">Ottawa</TH> <TH BGCOLOR="#FFFFE6" WIDTH="61%" HEIGHT="19" ALIGN="LEFT">is closed eastbound at Lancaster approximately 11 kilometres west of the Quebec border due to a collision. </TH> </TR> <!-- BEGIN ROW --> </TABLE></DIV> <DIV CLASS="content"> <P><SMALL>This information is provided as a public service. Although we endeavor to ensure that the information is as current and accurate as possible, errors do occasionally occur. Therefore, we cannot guarantee the accuracy of the information. Readers should, where possible, verify the information before relying on it.</SMALL></P> <H2 STYLE="font-size:1.1em;">Road information by phone</H2> <P><B>Provincial Call Centre:</B> 1-800-268-4686<BR><B>In GTA:</B> 416-235-4686</P> <P><B>Provincial TTY:</B> 1-866-471-8929<BR><B>Niagara Region TTY:</B> 905-704-2426</P><HR> <P CLASS="noBottom">See also:</P> <UL CLASS="noTop"> <LI><A HREF="traveller/conditions/index.html">Winter Road Condition Reports</A></LI> <LI><A HREF="traveller/compass/camera/pictures/tris/trismain.htm">Central Region Traffic Reports</A></LI> <LI><A HREF="winter.htm">Winter Driving in Ontario</A></LI> </UL> </DIV> <!-- TemplateEndEditable --> <HR><BR> <DIV CLASS="menuBottom"> | <A HREF="http://www.gov.on.ca/MBS/english/index.html">central site</A> | <!-- TemplateBeginEditable name="bottomFeedback" --> <A HREF="feedback/mtoinfo.htm">feedback</A> <!-- TemplateEndEditable --> | <A HREF="search.htm">search</A> | <A HREF="map.htm">site map</A> | <!-- TemplateBeginEditable name="bottomFrench" --> <A HREF="/french/traveller/conditions/rdclosure.htm"><SPAN LANG="fr-ca">français</SPAN></A> <!-- TemplateEndEditable --> | <BR>| <A HREF="index.html">Home</A> | <A HREF="about/">About the Ministry</A> | <A HREF="dandv/">Drivers & Vehicles</A> | <A HREF="traveller/">Traveller's Information</A> | <A HREF="trucks/">Trucks & Buses</A> | <A HREF="safety/">Road Safety</A> |<BR>| <A HREF="new/">What's New</A> | <A HREF="engineering/">Highways</A> | <A HREF="business/">Opportunities</A> | <A HREF="pubs/">Publications</A> | <A HREF="sites/sites.htm">Related Links</A> | <A HREF="faq/">FAQ</A> | <P><IMG SRC="images/wordmark.gif" WIDTH="404" HEIGHT="39" ALT="This site is maintained by the Government of Ontario, Canada." BORDER="0"><BR><BR> © 2003 <A HREF="http://www.gov.on.ca/MBS/english/common/queens.html">Queen's Printer for Ontario</A></P> </DIV> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted October 14, 2007 Share Posted October 14, 2007 Is there another file? I don't see where $strStart_Pattern and $strEnd_Pattern are set. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.