APC Powerchute Logfile Parsing with PHP

Associate
Joined
27 Apr 2004
Posts
2,377
I'm trying to knock up a simple script for my website using PHP that will allow me to display information from my UPS on the status page. I've tried using apcupsd twice but no luck (since it has web based add ons already).

So instead I'm going to use Powerchute and try and use the log files from it.

Here is a sample of the first few lines:

Date and Time Maximum Line Voltage Minimum Line Voltage Line Voltage Output Voltage Battery Voltage Output Frequency UPS Load UPS Internal Temperature Probe 1 Temperature Probe 1 Humidity Probe 2 Temperature Probe 2 Humidity
05/02/2006 19:37:12 N/A N/A 240.4 240.4 27.67 50.0 22.1 25.2 N/A N/A N/A N/A
05/02/2006 19:57:12 N/A N/A 240.4 240.4 27.67 50.0 22.1 25.6 N/A N/A N/A N/A

I'm using file() to fetch the lines into an array and then using array_shift() to get rid of the column titles. However, I'm stuck on how to separate the values to make them individually accesable (probably into a multi-dimensional array, not sure yet). I tried explode() with a space as the separator but of course this only separated the date from the rest.

I need a way to explode at any number of spaces, not just one. So, I ask, how can I do this?

Thanks, null :)
 
I'm not sure if this is what you want exactly, but i did this:
Code:
 $str = "05/02/2006 19:37:12 N/A N/A 240.4 240.4 27.67 50.0 22.1 25.2 N/A N/A N/A N/A";
 $parts = preg_split('/ /', $str);
 print_r($parts);

and got this:

Array
(
[0] => 05/02/2006
[1] => 19:37:12
[2] => N/A
[3] => N/A
[4] => 240.4
[5] => 240.4
[6] => 27.67
[7] => 50.0
[8] => 22.1
[9] => 25.2
[10] => N/A
[11] => N/A
[12] => N/A
[13] => N/A
)

Is that the kind of thing you mean?
 
Mine does that allready :p

I should have emphasised a bit more, the problem I'm having is because the spacing between values varies; either it's multiple space characters or a tab? (not sure how to differentiate).

So perhaps something that splits at tab characters.

Thanks, null :)
 
Back
Top Bottom