Break txt file into blocks, and each block into rows

Asked

Viewed 1,416 times

2

I have an "active.txt" file where you can find information blocks

Ex:

SACOLAO CENTER                                                
R RUA PAULINO MENDES LIMA,31                                  
CENTRO                                                        
45820440 EUNAPOLIS BA                                         

UNIQUE PISOS E REVESTIMENTOS                                  
R PAULINO MENDES LIMA,84                                      
CENTRO                                                        
45820440 EUNAPOLIS BA                                         

ZOO MANIA                                                     
AV PAULINO MENDES LIMA,185                                    
ANEXO I - CENTRO                                              
45820970 EUNAPOLIS BA 

I would like to read the entire file, and explode by block according to the white space between one and the other, for future I manipulate the lines of these blocks.

Ex:

$Array[1] = SACOLAO CENTER                                                
R RUA PAULINO MENDES LIMA,31                                  
CENTRO                                                        
45820440 EUNAPOLIS BA

And can display the lines of these blocks,

echo $Linha[0] = SACOLAO CENTER
echo $Linha[1] = R RUA PAILINO MENDES LIMA, 31

How could this be done?

2 answers

3

The white space between the blocks are two line breaks, ie, "\n\n". Then you can break the blocks so (considering the result of the file_get_contents is in $dados):

$blocos = explode("\n\n", $dados);

Then, to break each block you can do it:

foreach($blocos as &$bloco) {
    $bloco = explode("\n", $bloco);
}

Check the result with:

 var_dump($blocos);

3


Each of your blocks is separated by \n\r (\r\n -> CRLF), then you can explodir the blocks by \n\r. Each row of each block is separated by \n, then you can explodir each row of each block by new line (\n).

But you must observe with certainty what is the separation of the blocks, because in Linux can change.

Considering what you pasted above...

Demo

<?php

$ativos = file_get_contents("ativos.txt");

$blocos         = explode("\n\r", $ativos);
$linhas         = [];

foreach($blocos as $key_bloco => $bloco)
{
    $linhas_do_bloco      = array_values(array_filter(explode("\n", $bloco))); // remove linhas em branco e reordena keys
    $linhas[$key_bloco]   = $linhas_do_bloco;

    foreach($linhas_do_bloco as $key_linha => $linha_do_bloco)
    {
        echo "Bloco {$key_bloco} => Linha => {$key_linha} " . $linha_do_bloco . "\n\r";
    }
    echo "\n";
}

// exibindo uma linha específica de um bloco específico
// bloco 0
// linha 1
echo "Bloco 0 => Linha 1 => " . $linhas[0][1];

Upshot

Bloco 0 => Linha => 0 SACOLAO CENTER
Bloco 0 => Linha => 1 R RUA PAULINO MENDES LIMA,31
Bloco 0 => Linha => 2 CENTRO
Bloco 0 => Linha => 3 45820440 EUNAPOLIS BA

Bloco 1 => Linha => 0 UNIQUE PISOS E REVESTIMENTOS
Bloco 1 => Linha => 1 R PAULINO MENDES LIMA,84
Bloco 1 => Linha => 2 CENTRO
Bloco 1 => Linha => 3 45820440 EUNAPOLIS BA

Bloco 2 => Linha => 0 ZOO MANIA
Bloco 2 => Linha => 1 AV PAULINO MENDES LIMA,185
Bloco 2 => Linha => 2 ANEXO I - CENTRO
Bloco 2 => Linha => 3 45820970 EUNAPOLIS BA

Bloco 0 => Linha 1 => R RUA PAULINO MENDES LIMA,31
  • great, exactly that, however, in the execution here, presents the problem. Parse error: syntax error, Unexpected '[' in line "5" Line 5: $lines = []; Usage Appserver windows.

  • @Wilsonlavrador, if you put the string value $ativos = directly with the contents of the file ativos.txt, makes a mistake?

  • of the same error Parse error: syntax error, Unexpected '[' in... on line "5"

Browser other questions tagged

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