Remove spaces from a text file

1

How do I remove spaces from a text file? I have the following text file:

LC1 00019   1 31012012          00001                              00243206

I uploaded and put the following code:

$abrirArquivo = fopen($uploadArquivo, "r");
    while(!feof($abrirArquivo)){
        $ler = fgets($abrirArquivo,460);
        $quebrar = explode(" ",trim($ler));
        print_r($quebrar)."<br>";       
    }
    fclose($abrirArquivo);

But when I gave print_r() , it appeared like this:

  

Array ([0] => LC1 [1] => 00001 [2] = > [3] = > [4] = > 1 [5] = > 31012012   [6] = > [7] = > [8] = > [9] = > [10] = > [11] = > [12] = > [13] = > [14] = >   [15] = > 00001

Note that many keys are missing values. How do I resolve this problem?

    
asked by anonymous 15.10.2015 / 22:30

3 answers

2

To remove white space, use array_filter() :

$abrirArquivo = fopen($uploadArquivo, "r");
    while (!feof($abrirArquivo)) {
        $ler = fgets($abrirArquivo,460);
        $quebrar = explode(" ",trim($ler));
        $quebrar = array_filter($quebrar, function($var){return !is_null($var);});
        //print_r não necessita de "echo"
         print_r($quebrar);
        //se deseja converter para uma string, basta fazer um implode pelo separador:
         echo implode("", $quebrar);
    }
fclose($abrirArquivo);

However, I did not understand why you converted your string into an array, if you just want to remove the spaces from the contents of a file and show it, just do this:

$data = file_get_contents('seu_arquivo.txt');
$saida = preg_replace('/\s+/',' ', $data);

If you want to write to the file, edit your question to make it clearer. Now if you want to get some values from this array, just access the index, for example, to get 31012012, $quebrar[5] .

$quebrar = explode(' ', $saida);

For all lines:

foreach ($quebrar as $linha => $valor) {
    //aqui a posição da linha
    echo $linha . '<br>';
    //aqui o valor da linha
    echo $valor . '<br>';
}
    
19.10.2015 / 15:49
1

You are reading a file with fixed-length data, then you have to access the positions, you can not use explode , nor even assume that space is separator. There may be no space between different data, space may be part of the data. Forget that logic. it would have to be something like this (can improve):

$abrirArquivo = fopen($uploadArquivo, "r");
while(!feof($abrirArquivo)){
    $ler = fgets($abrirArquivo,460);
    $campo1 = trim(substr($ler, 0, 4));
    $campo2 = trim(substr($ler, 4, 8));
    $campo3 = ...;
    ...
}
fclose($abrirArquivo);

It's no use catching shortcuts. What you could do to make it simpler is to put the sizes or positions in an array and make a loop to automate all of this. Something like this:

$tamanhos = { 4, 8, 2, 18 ..... };
$abrirArquivo = fopen($uploadArquivo, "r");
while(!feof($abrirArquivo)){
    $ler = fgets($abrirArquivo,460);
    $campos = array();
    $posicao = 0;
    for ($i = 0; $i < sizeof($tamanhos); $i++) {
        $campos[] = trim(substr($ler, $posicao, $tamanhos[$i]));
        $posicao += $tamanhos[$i];
    }
}
fclose($abrirArquivo);
    
15.10.2015 / 22:41
1
preg_replace("#[^0-9|A-Z]#", "", $var);
    
15.10.2015 / 22:48