0

I'm trying to read data to php from a txt file, I already was able to do this, but now it's kind of different. the data file consists of rows looking like this:

10.09.16   0:05   16.7   16.8   16.7    70   11.2   3.2   ENE   0.27   6.4   ENE   16.7   16.4   16.4    ---   946.9  0.00   0.0     0     0.00      0     0.0  0.00   0.0   0.006   0.000   19.8    44    7.2   18.7   8.35 1.1146    15.6    0.00   112    2    100.0    5 


10.09.16   0:10   16.7   16.8   16.7    70   11.2   4.8     E   0.40   6.4     E   16.7   16.4   16.4    ---   946.8  0.00   0.0     0     0.00      0     0.0  0.00   0.0   0.006   0.000   19.7    43    6.8   18.6   8.25 1.1151    15.6    0.00   115    2    100.0    5 

as you see, the sepparation between the columns is not always the same, not for every column, and not even between rows.

now I have to get data from some specific columns. can someone help me with this? thanks!

  • 2
    Oh boy you'll need some regex work. – Phiter Nov 17 '16 at 10:15
  • Why can't you simply use a regular expression to split the rows by the white spaces? – arkascha Nov 17 '16 at 10:15
  • Please [edit] your question to show [what you have tried so far](http://whathaveyoutried.com). You should include a [mcve] of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Nov 17 '16 at 16:55

2 Answers2

0

You can read the file line by line, and then replace all the space by just one space (or other separator).

Then explode the line with the separator to get an array with each column.

Resource you could use:

Read file: check this answer

Replace space using str-replace

Explode in array using explode

Also, you could replace the space by some ; and use fgetcsv

Community
  • 1
  • 1
JérémyCasper
  • 182
  • 3
  • 17
0

Regular expressions are very powerful when it comes to matching and separating text sequences:

Simple approach if you don't know the number of columns in advance:

<?php

$input = <<<EOT
10.09.16   0:05   16.7   16.8   16.7    70   11.2   3.2   ENE   0.27   6.4   ENE   16.7   16.4   16.4    ---   946.9  0.00   0.0     0     0.00      0     0.0  0.00   0.0   0.006   0.000

10.09.16   0:10   16.7   16.8   16.7    70   11.2   4.8     E   0.40   6.4     E   16.7   16.4   16.4    ---   946.8  0.00   0.0     0     0.00      0     0.0  0.00   0.0   0.006   0.000

EOT;

$output = [];
foreach(explode("\n", $input) as $subject) {
  if (trim($subject)) {
    preg_match_all('/(?:([^\s]+)\s+)+/uU', $subject, $tokens);
    $output[] = $tokens[1];
  }
}

var_dump($output);

Much more elegant approach if you do know the number of columns in advance:

<?php

$input = <<<EOT
10.09.16   0:05   16.7   16.8   16.7    70   11.2   3.2   ENE   0.27   6.4   ENE   16.7   16.4   16.4    ---   946.9  0.00   0.0     0     0.00      0     0.0  0.00   0.0   0.006   0.000

10.09.16   0:10   16.7   16.8   16.7    70   11.2   4.8     E   0.40   6.4     E   16.7   16.4   16.4    ---   946.8  0.00   0.0     0     0.00      0     0.0  0.00   0.0   0.006   0.000

EOT;

preg_match_all('/(?:([^\s]+)\s+)+/uUm', $input, $output);
$output = array_chunk($output[1], 39);

var_dump($output);

I declared the input text inline to reduce complexity, you can read it from a file instead.

Both approaches will produce an identical output alone these lines:

array(2) {
  [0] =>
  array(39) {
    [0] =>
    string(8) "10.09.16"
    [1] =>
    string(4) "0:05"
    [2] =>
...
    [37] =>
    string(5) "100.0"
    [38] =>
    string(1) "5"
  }
  [1] =>
  array(39) {
    [0] =>
    string(8) "10.09.16"
    [1] =>
    string(4) "0:10"
    [2] =>
...
    [37] =>
    string(5) "100.0"
    [38] =>
    string(1) "5"
  }
}
arkascha
  • 41,620
  • 7
  • 58
  • 90