Break txt file into blocks, and each block in lines

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 blast by block according to the whitespace between one, and in the future I manipulate the lines of those blocks.

Ex:

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

And being able to display the rows of these blocks,

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

How could this be done?

    
asked by anonymous 10.07.2015 / 17:50

2 answers

3

Each block of yours is separated by \n\r ( \r\n -> CRLF ), then gives explodir blocks by \n\r . Each row of each block is separated by \n , then gives% to each row% of each block by new line ( explodir ).

But you should note for sure what the separation of the blocks is, as \n 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];

Result

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
    
10.07.2015 / 18:11
3

The white space between the blocks is two line breaks, that is, "\n\n" . So you can break the blocks like this (assuming the result of file_get_contents is in $dados ):

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

Then, to break each block you can do this:

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

Check the result with:

 var_dump($blocos);
    
10.07.2015 / 17:55