解析CSV文件

时间:2015-02-16 09:47:25

标签: php excel csv

我正在尝试将我的xls文件转换为csv并在php中解析它

以下是以逗号分隔的经过转换的cvs Windows转换

DFRWR2496W/BND*,,2.65 ,2.52 ,3.98 ,910,856,1126,
DFWR0006-RD.50W,,2.43 ,2.31 ,3.65 ,380,331,578,
DFWR0006-RD.75W,,3.50 ,3.33 ,5.25 ,490,419,775,
DFWR0006-RD1.0W,,4.44 ,4.22 ,6.66 ,610,520,972,

IN Php我试图将xxxx.csv文件设为

$ file_handle = fopen(“price.csv”,'r');     while(!feof($ file_handle)){         $ line_of_text [] = fgetcsv($ file_handle,1024,“\ r”);     }     FCLOSE($ file_handle);         return $ line_of_text [0];

然而$ line_of_text [0]给了我

["DFRWR2496W\/BND*,,2.65,2.52,3.98,910,856,1126","DFWR0006-RD.50W,,2.43,2.31,3.65,380,331,578","DFWR0006-RD.75W,,3.5,3.33,5.25,490,419,775","DFWR0006-RD1.0W,,4.44,4.22,6.66,610,520,972","DFWR0006-RD1.0Y,,4.44,4.22,6.66,610,520,972","DFWR0006W\/BND,,2.47,2.35,3.71,385,335,586","DFWR0007-RD.50W,,4.54,4.31,6.81,575,483,945","DFWR0007-RD1.0W,,5.28,5.02,7.92,675,568,1105","DFWR0007-RD1.0Y,,5.28,5.02,7.92,675,568,1105","DFWR0007W\/BND,,2.55,2.42,3.83,385,333,593","DFWR0008BD-RD.50W,,4.96,4.71,7.44,825,725,1229","DFWR0008-CSH1.0W,,4.62,4.39,6.93,1075,982,1451","DFWR0008-RD.50W,,4.81,4.57,7.22,1075,978,1467","DFWR0008-RD.75W,,4.81,4.57,7.22,1075,978,1467","DFWR0008-RD1.0W,,4.59,4.36,6.89,1075,982,1449","DFWR0008-TRD7.0W,,5,4.75,7.5,1075,974,1482","DFWR0008W\/BND,,2.23,2.12,3.35,460,415,642","DFWR0012BD-RD.50W,,4.56,4.33,6.84,580,488,951","DFWR0012P-RD.50TTR***,,4.56,4.33,6.84,835,743,1206","DFWR0012-RD.50TTR,,4.31,4.09,6.47,725,638,1076","DFWR0012-RD.50TY,,5.29,5.03,7.94,725,618,1156","DFWR0016W,,2.1,2,3.15,575,533,746","DFWR0028-PRD10x6.5W,,4.05,3.85,6.08,1000,918,"

现在不是一行

DFRWR2496W/BND*,,2.65 ,2.52 ,3.98 ,910,856,1126,

它似乎需要1024个字符?我怎么想像在excel中看到的那样解析它

1 个答案:

答案 0 :(得分:2)

fgetcsv没有检测到行结尾,在调用fopen之前添加它: ini_set('auto_detect_line_endings',true);

Processing CSV File in PHP that able to cater MS and UNIX Line Break