anthonyc Posted March 1, 2012 Share Posted March 1, 2012 Hello All, Please excuse my lack of knowledge in advance, I'm a beginner but pick things up relatively quickly, and tend to do things by trial and error. I would appreciate any help that anyone could offer with the following: I would like to retrieve two pieces of data from a "text file" and use them to calculate a result from a function I have in javascript. An example of the javascript code is: <script type="text/javascript"> function calculate() { var drybulb = document.calc_form.drybulb.value; var relhum = document.calc_form.relhum.value; var answer = ''; if (drybulb !== '' && relhum !== '') { answer = (0.567*drybulb*1) + (0.393 * (relhum*1/100 * 6.105 * Math.exp(17.27 * drybulb / (237.7 + drybulb*1)))) + 3.94; } document.calc_form.answer.value = answer; return false; } </script> Currently this is evaluated with entry from a simple html form, but I would like to have this performed "automatically". The "text file" that I would like to access is here, not on my server: http://www.bom.gov.au/fwo/IDV60901/IDV60901.94870.axf I would like to extract the "air_temp" and "rel_hum" values under the [data] section for use in my function. I just need the "most recent" values (i.e. first), not all those contained in the file. This is a single record from that section (sorry for the mess this no doubt makes): [data] sort_order,wmo,name[80],history_product[80],local_date_time[80],local_date_time_full[80],aifstime_utc[80],air_temp,apparent_t,cloud[80],cloud_base_m,cloud_oktas,cloud_type[80],cloud_type_id,delta_t,dewpt,gust_kmh,gust_kt,lat,lon,press,press_msl,press_qnh,press_tend[80],rain_trace[80],rel_hum,sea_state[80],swell_dir_worded[80],swell_height,swell_period,vis_km[80],weather[80],wind_dir[80],wind_spd_kmh,wind_spd_kt 0,94870,"Moorabbin Airport","IDV60901","27/10:00pm","20120227220000","20120227110000",18.8,19.3,"Partly cloudy",510,3,"-",-9999,0.8,17.5,13,7,-38.0,145.1,1013.5,-9999.0,1013.5,"-","11.4",92,"-","-",-9999.0,-9999,"10","-","S",11,6 Would it be possible to obtain these values using PHP?, and if so any advice on how this could be done (noting my minimal knowledge) would be greatly appreciated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/258019-reading-specific-data-from-a-text-file/ Share on other sites More sharing options...
dragon_sa Posted March 1, 2012 Share Posted March 1, 2012 Here you go use the variables $airTemp and $relHum as you like in your javascript as you like <?php $data=file('http://www.bom.gov.au/fwo/IDV60901/IDV60901.94870.axf'); foreach ($data as $line) { $info = explode(",", $line); if ($info[0] == '0') { $airTemp = $info[7]; $relHum = $info[25]; break; } } echo "Air Temperature is: $airTemp<br/>"; echo "Relative Humidity is: $relHum %"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/258019-reading-specific-data-from-a-text-file/#findComment-1322578 Share on other sites More sharing options...
anthonyc Posted March 1, 2012 Author Share Posted March 1, 2012 Thank you for your help, can't believe how fast that was and how simple you made it look. I have this working now, but might be back for more...... Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/258019-reading-specific-data-from-a-text-file/#findComment-1322699 Share on other sites More sharing options...
Naps Posted March 1, 2012 Share Posted March 1, 2012 Thanks, helpful to me too. Quote Link to comment https://forums.phpfreaks.com/topic/258019-reading-specific-data-from-a-text-file/#findComment-1322717 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.