Jump to content

reading a file with a lot of spaces between words


x_maras

Recommended Posts

Hi,

I want to read a file which is of unknown format.

It has experimental data in rows and columns.

 

The first like has the names of each column and then in each line

are some numbers.

 

Between every word or number result there are spaces, but in some

there is one space in other 2-3 and in some even 4-5.

 

an example of how the results are in the file is

 

167160 99999  19960101    57.0 24    52.7 24  1002.5 24  1000.7 24    6.0 24  10.1 24  22.0  30.9    61.5    53.6* 

167160 99999  19960102    57.3 23    49.5 23  1003.6 23  1001.8 23    7.9 23    3.3 23    7.0  999.9    62.6    51.8* 

167160 99999  19960103    54.9 24    45.7 24  1002.6 24  1000.8 24    7.6 24    8.4 24  15.0  999.9    57.9    51.8* 

 

 

So I dont know how i would be able to read this file and work with the columns

I want to.

 

I would appreciate some help in this.

 

 

You can do this using preg_split. In the code I put some examples so you can see how you would access the data from the $records array.

 

$file = <<<FILE
67160 99999  19960101    57.0 24    52.7 24  1002.5 24  1000.7 24    6.0 24   10.1 24   22.0   30.9    61.5    53.6*  
167160 99999  19960102    57.3 23    49.5 23  1003.6 23  1001.8 23    7.9 23    3.3 23     7.0  999.9    62.6    51.8*  
167160 99999  19960103    54.9 24    45.7 24  1002.6 24  1000.8 24    7.6 24    8.4 24   15.0  999.9    57.9    51.8*  
FILE;

//$file = file_get_contents('somefile.txt');
$rows = explode("\n", $file);
foreach($rows as $row)
$records[] = preg_split('~\s+~', $row);
echo $records[0][0]; // 67160
echo $records[0][2]; // 19960101
echo $records[1][1]; // 99999

The \s means whitespace character and the + means 1 or more. The tildes (~)'s are just delimiters, which are required with PCRE regular expressions. On a side note, the input you provided initially looks suspiciously like a tab delimited file. If it is then you could just use...

 

$rows = explode("\n", $file);
foreach($rows as $row) {
   $columns = explode("\t", $row);
   foreach($columns as $column) {
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.