Removing spaces from a text file

Asked

Viewed 1,340 times

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 a print_r(), appeared that way:

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 have gone without values. How do I solve this problem?

  • How do you want to read the string? pq explodes it by space?

  • I actually need to get just some information from this text file. For example: I need 31012012 and 00243206. I blew up to break and pick up inside an array, but I’m running into this challenge.

  • You can use the ID number, 3 and 5, ex, echo $quebra[3] .'#'. $quebra[5]; is continuation of that?

  • I’m sorry, I don’t understand your question...

3 answers

2

To remove whitespace, 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 don’t 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>';
}
  • Just one more question. My text file it brings with a very large spacing as posted above, but when testing the codes, I saw that positions can vary. Would there be any way to remove these spacings and put only one space between them, from which it would be easier to pick up an array without errors? For LC1 0019 1 31012012 00001 00243206

  • 1

    just change to: echo preg_replace('/\s+/',' ', $data); or in the above method: echo implode(" ", $quebrar);

  • Perfect. I used the date = file_get_contents('seu_file.txt'); echo preg_replace('/ s+/','', $data); Removed all spaces by placing only one space between each word. Now how do I pick up for example one of the words? Ex.: LC1 00001 1 31012012, I would like to take only 31012012.

  • 1

    The answer to that question is already written.

  • I’m sorry, Ivan. You’re right. I didn’t realize. His code worked perfectly, but to close with golden key, he is returning me only the first line.... has some way to return all lines?

1

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

$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 taking shortcuts. What you could do to simplify is to put the sizes or positions in one 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);

I put in the Github for future reference.

  • That function scanf doesn’t do anything like that?

  • No. I may even succeed in this case, but it’s the coincidence. What he has here (and I also know why he has asked another question about it) is a fixed-size file, any algorithm that takes information otherwise, is conceptually wrong and will only work by coincidence.

1

preg_replace("#[^0-9|A-Z]#", "", $var);
  • 1

    Note that in the question he breaks the elements into several parts. Missing break them so his answer is complete. Or right.

  • 1

    That sounds more like a piece of code to me.

Browser other questions tagged

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