webstar Posted April 22, 2007 Share Posted April 22, 2007 <?php //============ Local Functions ====================== function webfile($url) { // var $totalbuffer; $totalbuffer = ""; if (!($fp=fopen($url,"rb"))) { return "None"; } else { while ($buffer = fread($fp,1024)) { $totalbuffer .= $buffer; } return $totalbuffer; } } //end function // Gets a substring of the webpage for scraping of the data // $strFile ==> text webpage; $strStart_Pattern ==> start of the substring; $strEnd_Pattern ==> end of the substring function finddata($strFile,$strStart_Pattern,$strEnd_Pattern, $intStart_Position = 0) { $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 if } //end function // records the all events with their last updated time function event_log ($event) { } // end function event_log //============== Main program ===================== # ---------------------------------------------------------------------------------- # Configuration # Directory on the remote server. Be sure to include leading and trailing slashes! $remotedir = "/english/traveller/compass/camera/pictures/tris/"; # File on the remote server. No slashes! $remotefile = "durham.htm"; # Event type (Unscheduled, Scheduled) $event_type = "Unscheduled"; # Keyword to check for in response. $keyword = "Reference"; # Remote server address. $remoteserver = "http://www.mto.gov.on.ca"; # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "###@####"; # E-mail subject line. $emailsubject = "Road Alert" . date(' (G:i:s)'); # E-mail From name. $emailfromname = "WxSrv Roads"; # E-mail From address. $emailfromaddr = "####@###"; # End Configuration # ---------------------------------------------------------------------------------- // retrive the webpage $currentpage = webfile($remoteserver . $remotedir . $remotefile); // isolate the date $currentrow = finddata($currentpage,"<TABLE WIDTH=\"600\" CELLPADDING=\"0\" CELLSPACING=\"0\">","</TABLE>"); // futher isoloate for the last update $table_row = finddata($currentrow,'<TR>','</TR>'); $document_position = strpos($currentpage,$table_row); //================================================ if ( strpos($table_row,$event_type) ) { //Determin if an $event type occurs //Records the first starting location of the string $start_pos = strpos($table_row,"<P ALIGN=\"LEFT\">"); //Start of the loop for extracting events while ( $start_pos ) { //events are coded in the table row and start with the tags <P> and end with</P> $event = finddata($table_row,"<P ALIGN=\"LEFT\">","</P>",$start_pos); if ( strpos($event,"Reference ID:") ) { //Determine if this is and event with a reference ID: //Now we need to massage the string to get the info $event = strip_tags($event,'<BR>'); // Creates an array from the $event string based on the remain html tag <BR> # list($referenceID, $location, $description, $impact, $updated) = explode("<BR>",$event); $event_array = explode("<BR>",$event); //Loops through the array to print it out $new_event = true; foreach ($event_array as $item) { echo "$item<br>\n"; $message .= "$item\n"; } //end foreach echo "<br />"; echo "=====<br />"; $message .= "====-END-====\n\n"; //End of display loop } // end Reference ID $start_pos = strpos($table_row,"<P ALIGN=\"LEFT\">",$start_pos+1); // get next event } // end while loop //End of Event Loop // MY CHANGES $message = trim($message); $message = strip_tags($message); $message = str_replace("\r",'',$message); $message = str_replace("\n ",'',$message); # Check for keyword. if (!strstr($message, $keyword)) die("ERROR Code Word Not Found"); # Read the file containing the last response. $filename = $remotefile . '.txt'; $fp = fopen($filename, "r"); $contents = fread($fp, filesize($filename)); fclose($fp); # Check for changes. if ($contents == $message) { # There has not been a change. echo ("No Updates\r\n"); } else { # There has been a change. echo ("**UPDATES DETECTED**\r\n"); # Write the new file. $filename = $remotefile . '.txt'; $fp = fopen($filename, "w"); $write = fputs($fp, $message); fclose($fp); # Send the e-mail. $recipient = $emailrecip; $subject = $emailsubject; $body_of_email = $message; $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); } } else { //This is displayed when nothing is to report ***Can be deleted*** echo "No $emailsubject\n"; echo "<br>============<br>\n"; } // end Unscheduled Event Scrape // I COMMENTED THIS OUT BECAUSE OF THE EMAIL STUFF ABOVE // if ( strlen($message) ) { //must change to // mail($emailrecip, $emailsubject, $message); // } ?> And this is what it outputs. Reference ID: 10479 HWY 401 Eastbound Collector [ WHITES RD - LIVERPOOL RD ] Lanes affected: 1 right lane and right shoulder Event start date: 2007-04-22 Event start time: 01:31 Reason: Collision Traffic impact: Serious Last change: UPDATED 2007-04-22 03:43 ====-END-==== Is there a way to have lines 2 and 6 as part of the subject so it looked like this. Subject: MTO-DURHAM - Collision - HWY 401 Eastbound Collector [ WHITES RD - LIVERPOOL RD ] Quote Link to comment https://forums.phpfreaks.com/topic/48111-little-help-needed/ Share on other sites More sharing options...
fanfavorite Posted April 22, 2007 Share Posted April 22, 2007 $emailsubject .= " - ".$line6variable." - ".$line2variable; The . before the = allows you to add to the variable. Quote Link to comment https://forums.phpfreaks.com/topic/48111-little-help-needed/#findComment-235235 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.