0

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.'&deg;C<br>'; 

// ... 

Could anyone please teach me how to do this the way a php expert would do? Thanks in advance!

1 Answer 1

3

By the looks of it you can just explode on space and use list() to set the array to each variable.

list($udate, $utime, $temp, $hum, $dew, $wspeed, $wlatest, $bearing, $rrate, $rfall, $press, $currentwdir, $ptrend, $ttrend) = explode(" ", file_get_contents('realtime.txt'));

The order of the parameters in list should match what order the values are in the file.


An alternative method is to keep the array as an array but make is associative.

$keys = ['udate', 'utime', 'temp', 'hum', 'dew', 'wspeed', 'wlatest', 'bearing', 'rrate', 'rfall', 'press', 'currentwdir', 'ptrend', 'ttrend'];
$arr = array_combine($keys, explode(" ", file_get_contents('realtime.txt')));

echo $arr['udate']; //02/02/19

This means you can loop through the values and use a single line of code to output all values.

foreach($arr as $key => $val){
    echo $key . " is " . $val;
}
// udate is 02/02/19
// utime is 11:50:10
// temp is 11.1
// And so on

As you can see what you set the as the name in the $keys array is what is displayed.
So if you set "Date updated" as key you will get a nicer output.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you sooo much Andreas! it looks perfect and I am going to try it. The txt file has 58 keys so it will take me some time to rename them all to use your approach for a nicer output. I will let you know the resul later on.
It may not always be appropriate with the second method now that I think about it. Since you can't add "degrees Celsius" for example.
Thank you once again Andreas! The 2 solutions you presented here are exactly what I was looking for! However I do prefer #1, because it is much easier to echo single values (temperature, humidity, windspeed, etc) just when they are needed in the script. For other guys that will use #1 method, there is a coma missing in explode(" " file_get_contents('realtime.txt')); It should be explode(" " , file_get_contents('realtime.txt')); Once again Andreas, thank you soooo much you can't imagine how happy I am now :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.