How do I save a txt in ANSI with fwrite?

2

When I run the PHP code below

$arquivo = fopen("emails.txt","w");

while($dados = $cod_user->fetch_array()){ 
    $bla = $dados['descricao'];
    echo $dados["descricao"]."<br>";
    fwrite($arquivo,$bla);
}

It is automatically saving .txt in UTF-8 encoding, but I want it to save in ANSI.

My Locaweb provider has something to do?

    
asked by anonymous 08.03.2017 / 21:11

2 answers

3

You can try this

$bla = mb_convert_encoding($bla, 'UTF-8', 'codificação' );

Codificações de Caracteres Suportadas: codificação

http://www.php.com.br/index.doc.php?doc=php/mbstring.supported-encodings.html
    
09.03.2017 / 00:31
0

I think ANSI is actually referring to the characters compatible with iso-8859-1 or windows-1252 (or another one close to it), summarizing anything other than Unicode with accents latinos , this being the case you can try using iconv to convert

Using 'ISO-8859-1 // TRANSLIT'

TRANSLIT tries to convert from UTF-8 to iso-8859-1

 $bla = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $dados['descricao']);

Using 'ISO-8859-1 // IGNORE'

The IGNORE will convert all and if there are characters that can not it will ignore them:

 $bla = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $dados['descricao']);

Difference of IGNORE and TRANSLIT

For example the sign (euro), is "translated":

<?php
$text = "símbolo do Euro '€'.";

echo 'Original : ', $text, PHP_EOL;
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
echo 'IGNORE   : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;

It will turn:

Original : símbolo do Euro '€'.
TRANSLIT : símbolo do Euro 'EUR'.
IGNORE   : símbolo do Euro ''.

Resolving the connection

Maybe you can also solve in the mysqli API, just start the connection like this:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

if (!$mysqli->set_charset("latin1")) {
    printf("Error loading character set latin1: %s\n", $mysqli->error);
    exit;
}

Of course it is important to note that this will affect your page if you are also wanting to display something on the page, however you can switch between:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
    exit;
}

// ... Exibe algo na página vindo do banco aqui


//muda pra latin1 pra inicia a gravar
if (!$mysqli->set_charset("latin1")) {
    printf("Error loading character set latin1: %s\n", $mysqli->error);
    exit;
}

$arquivo = fopen("emails.txt","w");
while($dados = $cod_user->fetch_array()){ 
    $bla = $dados['descricao'];
    fwrite($arquivo,$bla);
}
fclose($arquivo);

//Restaura para utf8
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
    exit;
}
    
11.10.2017 / 22:33