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]"
I think your problem has to do with this: http://answall.com/questions/8429/php-echo-problema-specialcharacteristicsC3%A7
– Miguel
If you do that happens what ?
$registro = array_combine($cabecalho, utf8_decode($linha));
– Diego Souza
@Gumball if I do as I said, gives error: utf8_decode() expects Parameter 1 to be string, array Given
– Sr. André Baill
Got it. So, it needs to be string even. See the encoding that was saved the file, in Excel options.
– Diego Souza
You will have to go through the variable
$registro
with afor
orforeach
. Store in anotherarray
the data usingutf8_decode
.– Diego Souza