Intento leer un archivo CSV y repetir el contenido. Pero el contenido muestra los caracteres incorrectos.Problemas de UTF-8 al leer el archivo CSV con fgetcsv
Mäx Mustermann -> Mäx Müstermänn
codificación del archivo CSV es UTF-8 sin BOM (comprobado con Notepad ++).
Este es el contenido del archivo CSV:
"Mäx";"Müstermänn"
Mi script PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$handle = fopen ("specialchars.csv","r");
echo '<table border="1"><tr><td>First name</td><td>Last name</td></tr><tr>';
while ($data = fgetcsv ($handle, 1000, ";")) {
$num = count ($data);
for ($c=0; $c < $num; $c++) {
// output data
echo "<td>$data[$c]</td>";
}
echo "</tr><tr>";
}
?>
</body>
</html>
He intentado utilizar setlocale(LC_ALL, 'de_DE.utf8');
como se sugiere here sin éxito. El contenido sigue siendo incorrecto.
¿Qué me falta?
Editar:
Un echo mb_detect_encoding($data[$c],'UTF-8');
me UTF-8 UTF-8 da.
echo file_get_contents("specialchars.csv");
me da "Mäx";"Müstermänn"
.
Y
print_r(str_getcsv(reset(explode("\n", file_get_contents("specialchars.csv"))), ';'))
me da
Array ([0] => Mäx [1] => Müstermänn)
¿Qué significa?
Qué pasa cuando file_get_contents eco ("specialchars.csv")? ¿Qué sucede cuando imprime_r (str_getcsv (reset (explode ("\ n", file_get_contents ("specialchars.csv"))), ';'))? – Furgas
¡He actualizado mi pregunta! – testing