Issue with accentuation in CSV file export

Asked

Viewed 4,963 times

4

I read a CSV file as follows:

$delimitador = ';';
$cerca = '"';

// Abrir arquivo para leitura
$f = fopen($_FILES['uploadChange']['tmp_name'], 'r');
if ($f) {

    // Ler cabecalho do arquivo
    $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);

    // Enquanto nao terminar o arquivo
    while (!feof($f)) {

        // Ler uma linha do arquivo
        $linha = fgetcsv($f, 0, $delimitador, $cerca);
        if (!$linha) {
            continue;
        }

        // Montar registro com valores indexados pelo cabecalho
        $registro = array_combine($cabecalho, $linha);

        echo "<pre>";
        print_r($registro);
        echo "</pre>";

    }
    fclose($f);
}       

When reading and going to print_r() my return is OK, however, when there are accents in the lines it does not display correctly. How could I adjust?

Return example:

Array
(
    [Nome] => Andr� Baill
    [Idade] => 29
    [telefone] => (99) 9 9999-999-9999
    [email] => [email protected]
)
  • 1

    I think your problem has to do with this: http://answall.com/questions/8429/php-echo-problema-specialcharacteristicsC3%A7

  • 1

    If you do that happens what ? $registro = array_combine($cabecalho, utf8_decode($linha));

  • @Gumball if I do as I said, gives error: utf8_decode() expects Parameter 1 to be string, array Given

  • Got it. So, it needs to be string even. See the encoding that was saved the file, in Excel options.

  • 1

    You will have to go through the variable $registro with a for or foreach. Store in another array the data using utf8_decode.

2 answers

2

Andre, it seems your scenario is as follows:

  • The php file is saved in UTF8.
  • The csv file being opened is in ansi/iso.

As your output will be in UTF8 because your php file is in utf8, you have the following options:

1) Go to utf8 save the csv files.

2) Convert the csv file to utf8 and save it before importing.

3) Convert the values punctually. If you do so, it can be as follows:

Add:

    foreach($linha as $key => $value)
    {
        $linha[$key] = utf8_encode($value);
    }

After the excerpt:

    if (!$linha) {
        continue;
    }

Note: Converting something that is already in utf8 to utf8 will give problem. In case you are not sure that the file will come in ansi/iso you cannot quit by converting directly.

I use the following function when I want to check if a string is in utf8:

function is_utf8($str) 
{
    $c=0; $b=0;
    $bits=0;
    $len=strlen($str);
    for($i=0; $i<$len; $i++){
        $c=ord($str[$i]);
        if($c > 128){
            if(($c >= 254)) return false;
            elseif($c >= 252) $bits=6;
            elseif($c >= 248) $bits=5;
            elseif($c >= 240) $bits=4;
            elseif($c >= 224) $bits=3;
            elseif($c >= 192) $bits=2;
            else return false;
            if(($i+$bits) > $len) return false;
            while($bits > 1){
                $i++;
                $b=ord($str[$i]);
                if($b < 128 || $b > 191) return false;
                $bits--;
            }
        }
    }
    return true;
} 

Your adapted code:

<?php

function is_utf8($str) 
{
    $c=0; $b=0;
    $bits=0;
    $len=strlen($str);
    for($i=0; $i<$len; $i++){
        $c=ord($str[$i]);
        if($c > 128){
            if(($c >= 254)) return false;
            elseif($c >= 252) $bits=6;
            elseif($c >= 248) $bits=5;
            elseif($c >= 240) $bits=4;
            elseif($c >= 224) $bits=3;
            elseif($c >= 192) $bits=2;
            else return false;
            if(($i+$bits) > $len) return false;
            while($bits > 1){
                $i++;
                $b=ord($str[$i]);
                if($b < 128 || $b > 191) return false;
                $bits--;
            }
        }
    }
    return true;
} 


$delimitador = ';';
$cerca = '"';

// Abrir arquivo para leitura
$f = fopen('arquivo.csv', 'r');
if ($f) {

    // Ler cabecalho do arquivo
    $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);

    // Enquanto nao terminar o arquivo
    while (!feof($f)) {

        // Ler uma linha do arquivo
        $linha = fgetcsv($f, 0, $delimitador, $cerca);
        if (!$linha) {
            continue;
        }

        foreach($linha as $key => $value)
        {
            if(!is_utf8($value))
            {
                $linha[$key] = utf8_encode($value);
            }
        }

        // Montar registro com valores indexados pelo cabecalho
        $registro = array_combine($cabecalho, $linha);

        echo "<pre>";
        var_dump($registro);
        echo "</pre>";

    }
    fclose($f);
}       

csv file used for testing:

NOME;EMAIL
"André Baill";"[email protected]"
"João";"joã[email protected]"

-2

I solved it simply $str = mb_convert_encoding($str, "iso-8859-15")

Browser other questions tagged

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