Can't read csv (tab delimited) correctly

I have a simple csv file, which is a tab delimiter, which I should use as is, because it comes from somewhere, and I want to read it and paste it into my db. I used simple PHP code to read it.

if(($handle = fopen("var/import/MMT29DEC.csv","r"))!==FALSE){ /*Skip the first row*/ fgetcsv($handle, 1000,chr(9)); while(($data = fgetcsv($handle,1000,chr(9)))!==FALSE){ print_r($data[0]); } } 

When printing_r the data it shows as

 Array ( [0] => 01SATAPC [1] => 40ATAPC [2] => [3] => 21P [4] => SERIAL ATA POWER CABLE [5] => 0.00 [6] => 2.00 [7] => 0 [8] => Power Supplies [9] => SERIAL ATA POWER CABLE [10] => 4 TO 15 PIN 160MM [11] => [12] => [13] => [14] => MELBHO [15] => 0.000 [16] => [17] => Order to Order [18] => 4 [19] => 2013-01-18 ) 

What is the desired result, but when I go to access a specific column value using $ data ['index'], for example. $ data [8] or $ data [1], this strangely gives me values ​​for garbage, says that for some iterations it gives me the correct values, but after 10-15 lines it starts giving me some numbers and other column values ​​.. ... I don. I know what happens to this, as far as I know, this should be a formatting issue. I tried to open the file in excel and its fine ....

+4
source share
1 answer

@ravisoni are you sure the second parameter fgetcsv of 1000 is longer than the longest line in your file? Try setting it to 0, as the docs [php.net/fgetcsv] say, and see if that matters.

 if(($handle = fopen("var/import/MMT29DEC.csv","r"))!==FALSE){ /*Skip the first row*/ fgetcsv($handle, 0,chr(9)); while(($data = fgetcsv($handle,0,chr(9)))!==FALSE){ print_r($data[0]); } } 
+7
source

All Articles