Remove a part of the text inside a txt file

3

Colleagues.

I have the following code where I get a csv file through upload and store it in a txt file, from which you have to have the formation of the positions according to the database of a program. For this I am using the code below:

         $diretorio = 'arquivos/';
            $arquivo = $diretorio . basename($_FILES['userfile']['name']);

            if (move_uploaded_file($_FILES['userfile']['tmp_name'], $arquivo)){

               $abrirArquivo = fopen($arquivo, "r");

              while(!feof($abrirArquivo)) {
                    $ler = fgets($abrirArquivo,460);
                    $campo1 = substr($ler, 0, 8);
                    $campo2 = substr($ler, 9,10);
                    $campo3 = substr($ler, 20, 10);
                    $campo4 = substr($ler, 31, 22);
                    $campo5 = substr($ler, 40, 10);

$leituraFinal = "
$campo1          $campo2   $campo3                          $campo4                     $campo5<br>";

                //$trocar = str_replace("<br>","",$leituraFinal);
                $trocar = preg_replace("<br>",null,$leituraFinal);
                file_put_contents($diretorio . "JAN_2012.txt",$leituraFinal,FILE_APPEND);
                file_get_contents($diretorio . "JAN_2012.txt");

            }
     }
}

Legal. It works, at least in parts, because in the txt file it appears at the end of the line < br >.

    
asked by anonymous 19.11.2015 / 15:02

2 answers

4

If I understand correctly, you want to replace "null" with a line change in the txt file. I think for this you have to do the following:

$trocar = preg_replace("\n", null, $leituraFinal);

Replacing the previous code:

$trocar = preg_replace("<br>",null,$leituraFinal);
    
19.11.2015 / 15:14
0

In order for you to remove the br tag from the end of the txt file, you could do this by changing the following code in your code:

$trocar = preg_replace("<br>",null,$leituraFinal);

To:

$trocar = preg_replace('/<br>$/', '', $leituraFinal);

$trocar = preg_replace('/<br>/', '', $leituraFinal);
    
19.11.2015 / 20:32