webstar Posted January 15, 2012 Share Posted January 15, 2012 I'm trying to find the write coding to pull the First and Second words from the 2nd line and the entire 3rd line from this file and have them as $subject1 and $subject2 And I have no php background. http://www.weatherserver.net/text/CWTO/WOCN11.txt so $subject1 = WOCN11 CWTO and $subject2 = SPECIAL WEATHER STATEMENT Quote Link to comment https://forums.phpfreaks.com/topic/255096-small-help/ Share on other sites More sharing options...
litebearer Posted January 15, 2012 Share Posted January 15, 2012 Google ... PHP file() PHP file_get_contents() Quote Link to comment https://forums.phpfreaks.com/topic/255096-small-help/#findComment-1307990 Share on other sites More sharing options...
webstar Posted January 15, 2012 Author Share Posted January 15, 2012 If the data from the file is already passed to the php via this $fp = fopen('php://stdin','r'); $data = ""; while(!feof($fp)) { $data .= fgets($fp,4096); } fclose($fp); would file_get_contents still work on $data? Quote Link to comment https://forums.phpfreaks.com/topic/255096-small-help/#findComment-1307997 Share on other sites More sharing options...
litebearer Posted January 15, 2012 Share Posted January 15, 2012 file_get_contents('FILE_NAME_HERE') reads the entire file into one string; whereas, file('FILE_NAME_HERE') reads the file into an array. Each element of the array will contain one line from the file Quote Link to comment https://forums.phpfreaks.com/topic/255096-small-help/#findComment-1308000 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 <?php $file_array = file('http://www.weatherserver.net/text/CWTO/WOCN11.txt'); $subject1_words = explode(" ",$file_array[2]); $subject1 = $subject1_words[0] . " " . $subject1_words[1]; $subject2 = $file_array[3]; echo $subject1 . "<br />"; echo $subject2 . "<br />"; ?> Results: WOCN11 CWTO SPECIAL WEATHER STATEMENT Quote Link to comment https://forums.phpfreaks.com/topic/255096-small-help/#findComment-1308020 Share on other sites More sharing options...
webstar Posted January 21, 2012 Author Share Posted January 21, 2012 #!/usr/bin/php <?php # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "removed"; # E-mail From name. $emailfromname = "removed"; # E-mail From address. $emailfromaddr = "removed"; $fp = fopen('php://stdin','r'); $data = ""; while(!feof($fp)) { $new_data = fgets($fp,4096); $data .= $new_data; $data_array[$i++]=$new_data; } fclose($fp); $patterns = array(); $patterns[0] = '/CWTO/'; $patterns[1] = '/CWWG/'; $patterns[2] = '/CWVR/'; $patterns[3] = '/CWHX/'; $patterns[4] = '/CWUL/'; $replacements = array(); $replacements[0] = '-ON-'; $replacements[1] = '-Prairies-'; $replacements[2] = '-BC-'; $replacements[3] = '-Maritimes-'; $replacements[4] = '-QC-'; $data_array = preg_replace($patterns, $replacements, $data_array); $split_line = explode(' ', $data_array[2]); $subject = $split_line[0].' '.$split_line[1].' '.trim($data_array[3]); # Send the e-mail. $recipient = $emailrecip; $body_of_email = trim($data); $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; $header .= "\n"; $header .= "X-Priority: 1"; $header .= "\n"; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); ?> I kinda went a different direction with it. The first 3 arrays work but the 4th one never seems to fire and I was wondering if anyone can tell me why. Quote Link to comment https://forums.phpfreaks.com/topic/255096-small-help/#findComment-1309708 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.