Using as example the file data.txt with the following data:
0000000001 001 00000000000090000001 01/01/2014 00:00:38 1 0 5
0000000002 010 00000000000090000001 01/03/2015 00:00:38 1 0 5
0000000003 011 00000000000090000001 11/03/2014 00:00:38 1 0 5
0000000004 100 00000000000090000001 19/04/1999 08:50:38 1 0 5
Located in the same directory as the index php. with the following content:
<?php
// Abre arquivo com conteúdo
$arquivo = fopen("data.txt", "r");
// Percorre linha a linha do arquivo obtendo dados
while (!feof($arquivo)) {
$linha = fgets($arquivo);
$colunas = explode(" ", $linha);
$arrayColunas = [];
$i = 0;
foreach($colunas as $coluna) {
$arrayColunas[++$i] = $coluna;
if($i==4){
// Ajusta data
$arrayColunas[$i] = DateTime::createFromFormat('d/m/Y', $arrayColunas[$i])->format('Y-m-d');
}
}
echo json_encode($arrayColunas);
}
fclose($arquivo);
The code opens the file for reading and through a loop while gets line to line until you reach the end of the file (feof), for each row obtained we use the function explodes to divide its contents into an array where the columns are being identified by white space (could be other characters, for example ; ), the function foreach traverse each retrieved column by adding it to a new array, when the column is the date column, convert its format for insertion into a database.
In this example I am converting the array into a JSON and printing on screen, but the array can be used to perform database insertions or rewriting in files as needed.
Question missing an improvement in related items! Cite examples, cite real events, where data is obtained and everything that can help you solve your problem!
– novic
Certo Vírgilio!
– Paulo Ramos
Maybe you can use the PHP explode function http://php.net/manual/en/function.explode.php, first read the txt file and then separate the contents to each blank. In the link posted above you have some examples, see if it helps you.
– Douglas Horstmann