Write to txt file with PHP

2

I have a file txt but it is missing the nome in several lines of the file, I would like to name it according to the order number.

The logic would be to check the order number of the line that is unnamed in the case of the 203885 request, look for another line that has the same order number and that has a name, take this name and copy it to all numbers of equal orders. But order numbers that are not equal have different names. The column that represents the requests is column 2.

Original File txt:

100400;205537;30141;BALCAO 1 PORTA PER 400X670X500;MDF BP BRANCO;6;79281905;NOME1;0205537006
100400;203885;50058;PAI OPC EDIT LARGX ALTX 25;BLANCHE#695#320;18;79283198;NOME;0203885018
100400;205537;100001;EMBALAGEM DE FERRAGENS DIVERSAS;                                ;35;79283215;                              ;0203885035
100400;203885;101619;EMBALAGEM DE FUNDOS DORMITORIO;                                ;36;79283216;                              ;0203885036

After modified it should look like this:

100400;205537;30141;BALCAO 1 PORTA PER 400X670X500;MDF BP BRANCO;6;79281905;NOME1;0205537006
100400;203885;50058;PAI OPC EDIT LARGX ALTX 25;BLANCHE#695#320;18;79283198;NOME;0203885018
100400;205537;100001;EMBALAGEM DE FERRAGENS DIVERSAS;                                ;35;79283215;NOME1;0203885035
100400;203885;101619;EMBALAGEM DE FUNDOS DORMITORIO;                                ;36;79283216;NOME;0203885036

Just modify the file you do not need to print on the screen.

    
asked by anonymous 04.11.2016 / 15:00

2 answers

1

Kelvin would like to do the following, show how you are in txt and also how you want the result, so that I can understand and help you better. A while ago I made a post about UTF-8 fix which parses a file in question and fixes and creates a new one, I'll leave the link here may be helpful. link

I made a code that verifies the txt generates a vector with all the numbers and their names together of a new vector with all the information, after that I run again the vector already correcting the field that is missing, with that correcting the names. and finally create the items separated by ";"

Watch out for the code:

<?php

$file_name = "IMP.txt";

$file = fopen($file_name, "r") or die ("Arquivo não encontrado!");
$file_output = fopen("Output_".$file_name, "w") or die ("Arquivo não criado!");
// Gerando o vetor para tratamento
while(!feof($file)){    
    $strAnalise =  fgets($file) ;
    $strAnaliseArray = explode(";", $strAnalise);
    if(trim($strAnaliseArray[7]) != ''){
        $ArrayChaveNome[$strAnaliseArray[1]] = trim($strAnaliseArray[7]);
    }
    $strArrayDados[]=$strAnaliseArray;
}
// Corrindo o vetor;
foreach ($strArrayDados as $key => $value) {
    if(trim($value[7]) == ''){
        if(array_key_exists($value[1], $ArrayChaveNome)){
            $strArrayDados[$key][7]= $ArrayChaveNome[$value[1]];
        }
    }
}
// retornando a ";"
foreach ($strArrayDados as $key => $value) {
    $resultPontoVirgula.= implode(";", $value);
    $resultPontoVirgulaToTxt= implode(";", $value);
    fwrite($file_output, $resultPontoVirgulaToTxt);
}   
// var_dump($strArrayDados);
// var_dump($strAnaliseArray);
var_dump($resultPontoVirgula);

fclose($file_output);
fclose($file);

?>
    
04.11.2016 / 16:11
2

Test script

/*
Esse array simula a relação entre os códigos dos pedidos e os nomes
*/
$orders = array(
    '205537' => 'FULANO 1',
    '203885' => 'FULANO 2'
);

/*
Dados para teste
*/
$data = '100400;205537;30141;BALCAO 1 PORTA PER 400X670X500;MDF BP BRANCO;6;79281905;NOME;0205537006
100400;203885;50058;PAI OPC EDIT LARGX ALTX 25;BLANCHE#695#320;18;79283198;NOME;0203885018
100400;203885;100001;EMBALAGEM DE FERRAGENS DIVERSAS;                                ;35;79283215;                              ;0203885035
100400;203885;101619;EMBALAGEM DE FUNDOS DORMITORIO;                                ;36;79283216;                              ;0203885036';

/*
Essa é a parte que interessa.
*/
$arr = explode("\n", $data);
foreach ($arr as $k => $v) {
    $csv[$k] = str_getcsv($v, ';');
    $csv[$k][7] = $orders[$csv[$k][1]];
    $csv[$k] = implode(';', $csv[$k]);
}
$data = implode("\n", $csv);
echo $data;

Final implementation

$orders = array(
    '205537' => 'FULANO 1',
    '203885' => 'FULANO 2'
);

$file = '/local/do/arquivo.txt';
$arr = file($file);
foreach ($arr as $k => $v) {
    $csv[$k] = str_getcsv($v, ';');
    $csv[$k][7] = $orders[$csv[$k][1]];
    $csv[$k] = implode(';', $csv[$k]);
}
file_put_contents($file, (implode("\n", $csv));

Please be aware that this is not optimized for heavy files over 100MB, for example.

    
04.11.2016 / 18:16