Jump to content

string offset as an array


webstar

Recommended Posts

Hi all,

 

The script below is outputting this error. It just started today and I can't figure out how to correct it.

 

Fatal error: Cannot use string offset as an array in C:\WEATHERSCRIPTS\mto\test\roadclosed.php on line 37

 

<?php
// Declare variables //
$debug = false;
$url = "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm";
//$url = "./roadclosed1/rdclosure.htm";
$email_recip = "REMOVED";
#$email_recip = "REMOVED";
#$email_subject = MOVED TO LINE 47
$email_from = "WxServer Roads";
$email_from_addr = "REMOVED";
$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 CLASS=\"content\">", "</DIV>");  // All content is in the <DIV></DIV>
$content = find_data($content, "<TABLE", "</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)");
?>

Link to comment
https://forums.phpfreaks.com/topic/57668-string-offset-as-an-array/
Share on other sites

Little more detail. This script used to read the coding from the URL for anything between <DIV></DIV>

 

But the page has changed and now there is multiple DIV's in it now.

 

If you run the script you will see the error.

 

I have tried to edit the script at this point.

 

$webpage = read_web($url);

$content = find_data($webpage, "<DIV>", "</DIV>");  // All content is in the <DIV></DIV>

$content = find_data($content, "<TABLE", "</TABLE>");    // Look for the events HTML table

 

 

to have it start at different points but I still get the same error. It's likely something simple I overlooked.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.