Overwrite file snippet with php?

2

I do not know how to explain my problem, so I'll exemplify:

I am using PHP and would like to manipulate a .txt file of n lines. Each line has a default length of 20 characters.

For example ... the first 8 characters refer to a sending date (ddmmaaa), the 2 next to the source state (SP, RJ, ...), the other 2 to the destination state and the last 8 to product code.

Linha 1: 01012014RJPA00001472
Linha 2: 03121997MGRS00192010
...
Linha n: 28092012AMAC00000071

What I would like to know is how to overwrite one of these terms (not the entire line, not the whole file) and save the file.

Type: In line 2 of the file I want to change the source state from MG to MT without changing the other data in the file. As if I took the pointer to a position before the 'MG', I pressed the keyboard insert and typed 'MT'.

    
asked by anonymous 07.03.2014 / 13:35

3 answers

1

There are several ways ... What you asked for, I believe this is basically it:

<?php

// Tamanho padrão da sua linha
$linha_tamanho_default = 20;

// Array que define o inicio do valor na linha de acordo com o tipo de dado
$inicio = array(
    "DATA" => 0,
    "UF" => 8,
    "DESTINO" => 10,
    "CODIGO" => 12,
);

// Define o tamanho de cada item de acordo com o tipo de dado
$tamanho = array(
    "DATA" => 8,
    "UF" => 2,
    "DESTINO" => 2,
    "CODIGO" => 8,
);

// simulando o arquivo em uma string
// equivale a um arquivo txt com o conteudo do comentário abaixo:
/* 
01012014RJPA00001472
03121997MGRS00192010
28092012AMAC00000071
*/
$arquivo = "01012014RJPA00001472\n03121997MGRS00192010\n28092012AMAC00000071";


/***** Abaixo os parâmetros definem que a UF da 3ª linha será substituida por RJ */

// Define que TIPO de valor será substituido
$tipo = "UF";
// Define a linha que será alterada
$linha = 3;
// Define o NOVO valor
$valor = "RJ";

// Calcula a posição para a substituição
// usa $linha-1 (pois a contagem inicia em 0)
// usa $linha_tamanho_default+1 (pois inclui o \n)
// soma com o inicio do item que deseja substituir de acordo com a linha
$posicao = ($linha-1)*($linha_tamanho_default+1) + $inicio[$tipo];

// Retorna o arquivo   
$arquivo = substr_replace($arquivo, $valor, $posicao, $tamanho[$tipo] );

// Printa os resultados
echo $arquivo;

?>
    
07.03.2014 / 15:13
1

It would be interesting if you used regular expressions for this.

You said that your text file consists of lines in the following format:

  • 20 characters each
  • the first 10 characters are dates, irrelevant for our purposes
  • The next 2 characters are the UF, since we want to work
  • The following 8 characters are also irrelevant

So, the basic regular expression for each line would be:

/^(?P<data>.*){10}(?P<uf>.*){2}(?P<etc>.*){8}$/

To replace this data, just use the preg_replace function:

$siglaABuscar = 'MT';
$novaSigla    = 'MG';
$arquivo      = file_get_contents('arquivo.txt');
$arquivo      = preg_replace("/^(?P<data>.*){10}(?P<uf>$siglaABuscar){2}(?P<etc>.*){8}$/g", "$1$novaSigla$3", $arquivo);
file_put_contents('arquivo.txt', $arquivo);

PS: I wrote the head code, there might be some adjustments to be made.

    
07.03.2014 / 13:49
0

Here are some tips that can help

The methods fopen (), fread (), fgets (), fwrite (), fclose () are used to access and change files in your php script. link

Another option is to "pull everything to an array", make the changes, and then write the file again. (if there are too many checks and changes, it might be a good option)

$arrayDeLinhas = file($filename, FILE_IGNORE_NEW_LINES);

This will give you an array with the lines of the file, then you can handle the information as you wish.

Here you can find some method to help you handle the string. link

I believe that wordwrap and substr_replace should help you. But from now on is the organization of your algorithm, that's you!

    
07.03.2014 / 13:57