当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Parsing a CSV File

问题描述:

i am trying to convert my xls file into csv and parse it in php

Here is sampled converted cvs Windows conversion separated by commas

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 i am trying to pase the xxxx.csv file as

$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];

However $line_of_text[0] is giving me

["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,"

Now that is not one line

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

It seems its taking maybe 1024 characters? How am i suppose to parse it like it shows in excel

网友答案:

fgetcsv is not detecting line endings, add this before call to fopen: ini_set('auto_detect_line_endings', true);

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: