我有一个希腊语的csv文件。该文件的编码是ASCII。我的DB编码设置为utf8_general_ci。如何在utf8中转换每一行以便存储在DB中?
$file = fopen($_FILES['csv']['tmp_name'],"r");
$i=0;
while(! feof($file)){
$load = fgetcsv($file);
foreach($load as $csv){
$cv = explode(";", $csv);
if($i>=10){
$row = $students->addStudent($cv[3], $cv[2], $cv[4], NULL, $_COOKIE['school_id'], $_COOKIE['class_id']);
}
$i++;
}
}
在文本编辑器中打开csv文件并更改格式不适用,因为访问者应该这样做,而且很可能他们不知道如何。
也尝试了
/*switch(mb_detect_encoding ($_FILES['csv']['tmp_name'])){
case "ASCII":
echo "ASCII";
$file = iconv("ASCII", "ISO-8859-7", $_FILES['csv']['tmp_name']);
break;
case "UTF-8":
echo "UTF-8";
$file = $_FILES['csv']['tmp_name'];
break;
default:
$file = utf8_encode($_FILES['csv']['tmp_name']);
break;
}*/
但没有结果
答案 0 :(得分:0)
试试这个
while(! feof($file)){
$csv = utf8_encode(fgets($file));
$cv = explode(";", $csv);
if($i>=10){
$row = $students->addStudent($cv[3], $cv[2], $cv[4], NULL, $_COOKIE['school_id'], $_COOKIE['class_id']);
}
$i++;
}
答案 1 :(得分:0)
我将对文件进行编码,从greek-charset到utf-8,然后将它们存储在DB中。 也许,它不起作用,因为我没有正确的编码,这工作
$file = file_get_contents($_FILES['csv']['tmp_name']);
$file = iconv('ISO-8859-7','UTF-8', $file);
file_put_contents($_FILES['csv']['tmp_name'], $file);
$filed = fopen($_FILES['csv']['tmp_name'],"r");
$i=0;
while(! feof($filed)){
$load = fgetcsv($filed);
foreach($load as $csv){
if($i>=10){
$cv = explode(";", $csv);
$row = $students->addStudent($cv[3], $cv[2], $cv[4],'', $_COOKIE['school_id'], $_COOKIE['class_id']);
}
$i++;
}
}
答案 2 :(得分:-1)
最好的方法是使用array_map()函数。即
$array = array_map("utf8_decode", $array);