使用PHP将文本文件中的每一行转换为JSON

So I got an application which generates a log like this:

INFO:root:14/09/2016 13:44:13: Temp: 16.0 C  Humidity: 95.0 % 

INFO:root:14/09/2016 13:46:13: Temp: 25.0 C  Humidity: 78.0 % 

INFO:root:14/09/2016 13:48:14: Temp: 25.0 C  Humidity: 78.0 % 

And I'm building a simple website which aims to show this log and be able to sort it. I'd thought I'd read the log file with the help of PHP which should convert it into a JSON Array so it's easier to handle for my Javascript. The only problem is that I can't seem to get it working. I have tried alot of example code, many from Stackoverflow but none of it seems to work. I'm kinda new to PHP to so I'm not sure what to do here.

These are some things I've tried:

$logFile = file_get_contents( "../../../home/shares/flower_hum/humid.log" ); 
//*Did not work
$json = json_decode(file_get_contents($file), true);
//*Did not work
$lines = file($logFile, FILE_IGNORE_NEW_LINES)
//*Did not work
$fp = @fopen($filename, 'r'); 
if ($fp) {
$array = explode("
", fread($fp, filesize($filename)));
}

The format I want as JSON is something like this:

"log":[
{"14/09/2016 13:54:21":"Temp: 26.0 C":"Humidity: 76.0 %"}, 
{"14/09/2016 13:56:21":"Temp: 25.0 C", "Humidity: 75.0 %"} 
]

I hope my example is better, I understand this is hardcoded, but it works. It return just an array, but you can convert it to JSON.

$parsedLine = 'INFO:root:14/09/2016 13:44:13: Temp: 16.0 C  Humidity: 95.0 %';
function parseLogLine($parsedLine) {
    $parsedLine = substr($parsedLine, 5);
    $parsedData = explode("T", $parsedLine);
    $rootData = explode(":", $parsedData[0]);
    $parsedLine = "T".$parsedData[1];
    // root data
    $rootKey = $rootData[0];
    unset($rootData[0]);
    $rootData = implode(":", $rootData);
    $result = [
        $rootKey => $rootData
    ];
    // other
    $parsedData = explode("H", $parsedLine);
    $parsedData[1] = "H".$parsedData[1];
    foreach($parsedData as $value) {
        $vData = explode(":", $value);
        $result[trim($vData[0])] = trim($vData[1]);
    }
    return $result;
}

Your data are not JSON, it seems to be log file where each line containes a set of data.

You need to open it, then extract the data you need with a regexp, and store it in an array before using it :

$lines = file($logFile, FILE_IGNORE_NEW_LINES);

$data = array();
$pattern = '@INFO:root:(\d{2}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}):\sTemp:\s(.+)\sHumidity:\s(.+)@';
foreach($lines as $line){
    if(preg_match($pattern, $line, $matches)){
        $data[] = array(
            'date' => $matches[1],
            'temp' => $matches[2],
            'humidity' => $matches[3],
        );
    }
}

You will get this array :

array (
  0 => 
  array (
    'date' => '14/09/2016 13:44:13',
    'temp' => '16.0 C ',
    'humidity' => '95.0 % ',
  ),
  1 => 
  array (
    'date' => '14/09/2016 13:46:13',
    'temp' => '25.0 C ',
    'humidity' => '78.0 % ',
  ),
  2 => 
  array (
    'date' => '14/09/2016 13:48:14',
    'temp' => '25.0 C ',
    'humidity' => '78.0 % ',
  ),
)

You can then convert it to JSON :

echo json_encode($data);

NB: There is probably better regexp to do this job.