我正在尝试做一个简单的PHP函数,将.csv文件放入数据库中。问题是,文件是用分号分隔的,由于某种原因,当一个单元格包含逗号时,它会忽略后面的所有内容
例如.csv文件
manufacturer;comment;year
toyota;good car, bad color;1997
当使用print_r()
功能打印时,显示如下
[0] => Array
(
[0] => manufacturer
[1] => comment
[2] => year
)
[1] => Array
(
[0] => toyota
[1] => good car
)
编辑
这是我正在使用的代码。
//get the contents of the .csv file
$filepath = './files/csvfile.csv'
$fileopen = fopen($filepath,"r");
//create an empty array
$csv = array();
//go through the .csv file with fgetcsv()
while(($line = fgetcsv($fileopen,";")) !== FALSE)
{
$line = explode(";",$line[0]);
$csv[] = $line;
}
//print the result
echo "<pre>";
print_r($csv);
echo "</pre>";
fclose($fileopen);
将分隔符作为第三个参数
http://php.net/manual/en/function.fgetcsv.php
while(($line = fgetcsv($fileopen, 0, ";")) !== FALSE)
我认为你也不需要爆炸线。