Mount array with txt data

Asked

Viewed 853 times

5

I’m trying to find a way to assemble a table with the data of a txt, but I don’t know how to separate the values in the array, since they are separated by several spaces, but are identified by fields (Name: , Code: , ...);

Example of a txt file:

 Código: 808                         Nome Abreviado: 091/0007756       Cliente Cobrança: 808
Nome: Nome da empresa                          Representante: 8
              Endereço: AV.2422                                                Bairro: Jardim
                Cidade: Jacare                         UF: SP                    Caixa Postal:
                  País: BRASIL                                 CEP: 12222222                  Zip Code:
           Telefone[1]: 00-0000-0000                        Telex:                            CGC/CPF: 123.321.123.4
        Ramo Atividade:                                      Grupo: 99                Data Implantação: 03/09/2007
             Categoria:                        Inscrição Municipal:
    Inscrição Estadual: 111.222.333.-74
                E-mail:  [email protected]
  • Each field is in a row or there is a character limitation per column?

  • The fields are online, exactly the way it is in the post, in the first line has the Code, abbreviated name and client, in the next has the Name, representative and so on. What you have in common among the fields is :.

  • I find it difficult a parser for this case, how to define the end of the field and start of the next one? There are tabulations for the separation of fields?

  • The worst part is, it doesn’t exist. I did a test here and even gave to separate the fields based on :, which resulted in:Array ( [0] => Code [1] => 747 Abbreviated Name [2] => 2M Customer Billing [3] => 747 )

  • The array cannot be exactly the nome dos campos + : and then give a Trim in the content that is between this? It’s just an idea that came in my imagination !!!

  • checks the pattern used by the program q exported this file, then applies reverse engineering

  • 1

    This is a bank shipping file ?

  • Yes, this is a result of a program that queries in database, but not know how it works, my access to it is limited only to the result that is txt file.

  • If possible, post an example of functional code you have used, helps a lot to formulate a response.

  • @Thiago if this is a shipping or bank return file you are picking up the wrong file, this file is not made for machines, any parse there is uniform to fail since some name may have ":" or two spaces and there is no marking or escape character. I suggest you contact the bank to know how to recover the correct file.

Show 5 more comments

1 answer

1


Based on your text file, I was able to split into an array as you need:

$txt = ' Código: 808                         Nome Abreviado: 091/0007756       Cliente Cobrança: 808
Nome: Nome da empresa                          Representante: 8
              Endereço: AV.2422                                                Bairro: Jardim
                Cidade: Jacare                         UF: SP                    Caixa Postal:
                  País: BRASIL                                 CEP: 12222222                  Zip Code:
           Telefone[1]: 00-0000-0000                        Telex:                            CGC/CPF: 123.321.123.4
        Ramo Atividade:                                      Grupo: 99                Data Implantação: 03/09/2007
             Categoria:                        Inscrição Municipal:
    Inscrição Estadual: 111.222.333.-74
                E-mail:  [email protected]';

// identificar campos em branco, campos com 2 espaços após o : e separar os campos com uma barra em pé |
$txt = preg_replace(array('/:[\s]{3,}/','/:[\s]{1,2}/','/[\s]{2,}|[\r\n]/'), array(':NULL|',':','|'), $txt);

// fatiar no : e na |
$campos = preg_split("/[:\|]/", $txt);

// rodar todo o array fatiado de 2 em 2, populando o array final
for($i = 0; $i < sizeof($campos);$i += 2)
    $data[trim($campos[$i])] = ($campos[$i+1] == 'NULL' ? NULL : trim($campos[$i+1]) );

print_r($data);

/* retorno:

Array
(
    [Código] => 808
    [Nome Abreviado] => 091/0007756
    [Cliente Cobrança] => 808
    [Nome] => Nome da empresa
    [Representante] => 8
    [Endereço] => AV.2422
    [Bairro] => Jardim
    [Cidade] => Jacare
    [UF] => SP
    [Caixa Postal] => 
    [País] => BRASIL
    [CEP] => 12222222
    [Zip Code] => 
    [Telefone[1]] => 00-0000-0000
    [Telex] => 
    [CGC/CPF] => 123.321.123.4
    [Ramo Atividade] => 
    [Grupo] => 99
    [Data Implantação] => 03/09/2007
    [Categoria] => 
    [Inscrição Municipal] => 
    [Inscrição Estadual] => 111.222.333.-74
    [E-mail] => [email protected]
)
*/
  • I had commented that the business was kind of this side of putting the information as array. Bravoooooooooo

Browser other questions tagged

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