Answer the question
In order to leave comments, you need to log in
How to output csv file to utf-8 using iconv?
Hello.
There is a csv file in windows-1251 encoding. I tried 2 options for converting to utf-8:
<?php
$row = 1;
if (($handle = fopen("111.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo iconv("UTF-8", "Windows-1251", $data[$c]) . "<br />\n";
}
}
fclose($handle);
}
<?php
header('Content-Type: text/html; charset=UTF-8');
$row = 1;
if (($handle = fopen("111.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$data = array_map("utf8_encode", $data);
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Answer the question
In order to leave comments, you need to log in
Solution
1) Convert to UTF-8 (without BOM) in Notepad++
2) Given code
$row = 1;
if (($handle = fopen("1114.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question