How to format a txt file by inserting columns and changing date and time format with php script

Asked

Viewed 1,221 times

1

I have text file generated from an access control device, when I download the file it comes without columns of identification, what do I need to do? Insert columns and reset the date format so that the file can be imported into mysql. This way I can visualize: Thanks for the help!

This is a part of the file, a row with a total of 8 columns :

0000000001 001 00000000000090000001 01/01/2014 00:00:38 1 0 5
  • 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!

  • Certo Vírgilio!

  • 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.

2 answers

0

This solution should help you:

<?php
$linha  = "0000000001 001 00000000000090000001 01/01/2014 00:00:38 1 0 5";
$partes = explode(" ", $linha);
$i = 0;
foreach ($partes as $campo) {
    echo 'campo' . ++$i . ' = ' .  $campo . "\n"; 
}

Results in:

campo1 = 0000000001
campo2 = 001
campo3 = 00000000000090000001
campo4 = 01/01/2014
campo5 = 00:00:38
campo6 = 1
campo7 = 0
campo8 = 5

In practice, just take each element of the array $partes and place within each specific field you desire. Ex:

mysql_campo1 = $partes[0];
mysql_campo2 = $partes[1];
mysql_campo3 = $partes[2];
.
.
.

See working in https://ideone.com/WJEeDy

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.