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– bfavaretto
The
"\t"
is equivalent to a TAB of the ASCII table?– Moises Pequeno
Yeah, consider it a shortcut.
– bfavaretto