我有一个试图读取的CSV文件,但是某些字段值包含逗号,这使我的代码无效。我正在使用fgetcsv读取文件。
我正在读取的csv文件样本
"Stock Number","product","title","size","description","price"
"195642","T-Shirt","100% cotton classic fit","XL","Cotton shirts breathe better than clothing made with synthetic materials, and are less prone to retaining odors and losing shape.We offer a wide variety of cotton t-shirts from leading brands such as Fruit of the Loom, Hanes, New Balance, and more.","6.99"
问题在于描述字段中的值包含逗号。
// open file
$txtfile = fopen('../folder/where/file.csv', 'r');
// skip first line (header)
fgetcsv($txtfile, 10000, ",");
while (($line = fgetcsv($txtfile)) !== FALSE) {
}
// close file
fclose($txtfile);
在while循环中,我将所有CSV文件中的信息写入更大的CSV文件中,然后插入数据库中。
我遇到的问题是我的数组在描述字段中被逗号引起。例如,这是描述之一
"Cotton shirts breathe better than clothing made with synthetic materials, and are less prone to retaining odors and losing shape.We offer a wide variety of cotton t-shirts from leading brands such as Fruit of the Loom, Hanes, New Balance, and more."
当我的代码读取文件并且尝试输出$ line [4]的值时,我得到
Cotton shirts breathe better than clothing made with synthetic materials
然后是$ line [5],应该是价格输出
and are less prone to retaining odors and losing shape.We offer a wide variety of cotton t-shirts from leading brands such as Fruit of the Loom
答案 0 :(得分:0)
尝试指定附件字符。我在手册中看不到有默认附件的任何内容:
fgetcsv($txtfile, 10000, ",", '"');