Separate fields by TAB

Asked

Viewed 245 times

0

I’m making an appointment SQL and returning the results in a file . txt, so far so good!

It has now been requested that the contents of the file be separated by TAB (ASCII 9 ).

I did a lot of research and saw that I need to put the chr(9) representing the TAB, but I am not able to apply it to my code (very simple, by the way). Who can help me, thank you.

Below is the code I’m using:

if ($resultado > 0 ) {
while($mostrar = mysqli_fetch_array($sql)) {
$cpf=$mostrar["CodCgcCpf"];
$nome=$mostrar["Nome"];
$email=$mostrar["email"];
$endereco=$mostrar["Endereco"];
$numero=$mostrar["numero_predio"];
$complemento=$mostrar["complemento"];
$bairro=$mostrar["Bairro"];
$cidade=$mostrar["Cidade"];
$uf=$mostrar["UF"];
$cep=$mostrar["Cep"];
$telefone=$mostrar["Fone"];

$conteudo = "";
$conteudo .= "".PHP_EOL;
$conteudo = "$cpf;$nome;$email;$endereco;$numero;$complemento;$bairro;$cidade;$uf;$cep;$telefone ";
$conteudo .= "".PHP_EOL;
$name = "clientes.txt";
$file = fopen ($name, 'a+'); 
fwrite($file, $conteudo);
fclose($file);
}
} else {
}

I did it this way too.

function truncate( $string, $max_length )
{
   return substr( ( string ) $string, 0, ( int ) $max_length );
}

function prepare_string_to_column( $string, $limit )
{
   return ( str_pad( ( string ) $string, ( int ) $limit ) );
}

$fp = fopen( 'clientes.txt', 'a' );


$line1 = array(
   prepare_string_to_column( truncate( 'CPF', 11 ), 13 ),
   prepare_string_to_column( truncate( 'NOME', 30 ), 32 ),
   prepare_string_to_column( truncate( 'EMAIL', 30 ), 32 ),
   prepare_string_to_column( truncate( 'ENDERECO', 40 ), 42 ),
   prepare_string_to_column( truncate( 'NUMERO', 6 ), 8 ),
   prepare_string_to_column( truncate( 'COMPLEMENTO', 11 ), 13 ),
   prepare_string_to_column( truncate( 'BAIRRO', 15 ), 17 ),
   prepare_string_to_column( truncate( 'CIDADE', 15 ), 17 ),
   prepare_string_to_column( truncate( 'UF', 2 ), 4 ),
   prepare_string_to_column( truncate( 'CEP', 10 ), 12 ),
   prepare_string_to_column( truncate( 'TELEFONE', 12 ), 14 )


);

fwrite( $fp, implode( null, $line1 ) . PHP_EOL );

while($mostrar = mysqli_fetch_array($sql)) {

$cpf=$mostrar["CodCgcCpf"];
$nome=$mostrar["Nome"];
$email=$mostrar["email"];
$endereco=$mostrar["Endereco"];
$numero=$mostrar["numero_predio"];
$complemento=$mostrar["complemento"];
$bairro=$mostrar["Bairro"];
$cidade=$mostrar["Cidade"];
$uf=$mostrar["UF"];
$cep=$mostrar["Cep"];
$telefone=$mostrar["Fone"];

$line2 = array(


   prepare_string_to_column( truncate( "$cpf", 11 ), 13 ),
   prepare_string_to_column( truncate( "$nome", 30 ), 32 ),
   prepare_string_to_column( truncate( "$email", 30 ), 32 ),
   prepare_string_to_column( truncate( "$endereco", 40 ), 42 ),
   prepare_string_to_column( truncate( "$numero", 6 ), 8 ),
   prepare_string_to_column( truncate( "$complemento", 11 ), 13 ),
   prepare_string_to_column( truncate( "$bairro", 15 ), 17 ),
   prepare_string_to_column( truncate( "$cidade", 15 ), 17 ),
   prepare_string_to_column( truncate( "$uf", 2 ), 4 ),
   prepare_string_to_column( truncate( "$cep", 10 ), 12 ),
   prepare_string_to_column( truncate( "$telefone", 12 ), 14 )

);


fwrite( $fp, implode( null, $line2 ) . PHP_EOL );

}
fclose( $fp );
  • Use "\t" tabby

  • The "\t" is equivalent to a TAB of the ASCII table?

  • Yeah, consider it a shortcut.

1 answer

0

I tested some forms, concatenating or not:

$tab=chr(9).chr(9).chr(9);#aqui vai quantas tabs vc qser
$conteudo = "";

while($mostrar = mysqli_fetch_array($sql)){
    $conteudo .= "".PHP_EOL;
    $conteudo .= "$cpf;$tab$nome;$tab$email;$tab$endereco;$tab$numero;$tab$complemento;$tab$bairro;$tab$cidade;$tab$uf;$tab$cep;$tab$telefone ";
    $conteudo .= "".PHP_EOL;
}
$name = "clientes.txt";
$file = fopen ($name, 'a+'); 
fwrite($file, $conteudo);
fclose($file);

Note that in $tab i defined the value of chr(9), which is the ASCII for tab, I added between the variable fields $conteúdo and printed out the result like this:

cpf;    nome;   email;  endereco;   numero; complemento;    bairro; cidade; uf; cep;    telefone 

Another thing I noticed was that there in the code you shared, the variable $conteúdo right after the first PHP_EOL was not being concatenated consisting of only one = without the ., I took the liberty of adding it to my reply.

  • Thank you, that’s right! the result came out like this... CPF NOME EMAIL ENDERECO NUMERO COMPLEMENTO BAIRRO CIDADE UF CEP TELEFONE 11111111111 TESTE DA SILVA MESQUITA RUA CORONEL MEIRELES 1850 PENHA SAO PAULO SP 03612000 2244024 I could align in columns?

  • as well as align the columns, they are not in order and in different rows?

  • I’d like it to look something like this. https://www.researchgate.net/figure/Figura-4-Amostra-do-layout-inicial-dos-arquivos-TXT-Tomando-se-asexample-a-primeira_fig3_327057857

  • Ué friend, just put more tabs between the fields, I will edit

  • So I believe that it will not work this way, because the person who will read, will identify the separation of the tab as another field and in the example you gave, would have 3 tabs ie, three blank fields!

  • i wanted to set a size even for column Ex: column `CPF 11 characters - Column Name: 30 characters and so on and at the end of these characters a tab... I managed to make up the columns there in my question . But I could not define the tab in the concept I used

Show 1 more comment

Browser other questions tagged

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