Problem with accentuation in CSV file export

4

I made a reading of 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 on 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]
)
    
asked by anonymous 23.12.2016 / 17:51

1 answer

0

André, it looks like 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) Start saving in utf8 the csv files.

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

3) Convert the values on time. If it is to do this, 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. If you are not sure that the file will come in ansi / iso you will not be able to convert it 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 customized 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 to test:

NOME;EMAIL
"André Baill";"[email protected]"
"João";"joã[email protected]"
    
02.01.2017 / 12:10