I have a txt file ('realtime.txt') containing data from a weather station. The values are separated by a blank space and look like this:
02/02/19 11:50:10 11.1 60 3.6 23.6 19.4 338 0.0 1.5 1021.4 NNW +0.3 -1.4 ...
I would like to retrive each one of these values and assing it to a variable, to be echoed when needed in a php script. Using the same order those variables would be something like this:
$udate $utime $temp $hum $dew $wspeed $wlatest $bearing $rrate $rfall $press $currentwdir $ptrend $ttrend ...
With my begginner's php knowledge I managed to do it with a very odd solution that I am sure will make any php expert smile but... it's working if... :-) if number of characters doesn't change!!! If for instance, temperature changes from 11.9ºC to 9.5ºC everything gets messed up because there is one character less when counting!
<?php
// starting from caracter n read following i bytes
$udate = file_get_contents('realtime.txt', FALSE, NULL, 0, 8);
$utime = file_get_contents('realtime.txt', FALSE, NULL, 9, 8);
$temp = file_get_contents('realtime.txt', FALSE, NULL, 18, 4);
// ...
echo 'updated @: '.$udate.' '.$utime.'<br>';
echo 'temperature is: '.$temp.'°C<br>';
// ...
Could anyone please teach me how to do this the way a php expert would do? Thanks in advance!