Explode statement in .csv file issues

I’m having a few problems exploding data in a .csv file to read into a database.

This is the structure of the .csv file:

Stock ID,VRM,Manufacturer,Model,Trim,Door,Body,Colour,Transmission Speed,Transmission Type,Fuel Type,Engine Size,Year of manufacture,Reg letter/number,No. of keepers,Price,VAT qualifying indicator,Mileage,1st registration date,Vehicle description,Narrative,Standard Options,Additional Options,Image 1 url,Image 2 url,Image 3 url,Image 4 url,Image 5 url,Image 6 url,Image 7 url,Image 8 url,Image 9 url,Image 10 url,Image 11 url,Image 12 url
18109357,TEST,Suzuki,Alto,SZ4,5 Door,Hatchback,Grey,4 speed,Automatic,Petrol,996,2013,13,1,7495,N,3412,25/06/2013,Suzuki Alto 1.0 SZ4 5 Door Hatchback,“ABS(Anti-Lock Brakes),Adjustable Steering Column,Air Conditioning,Alloy Wheels,Body Coloured Bumpers,Cloth Interior,Cup Holder,Driver’s Air Bag,Electric Windows,Electronic Brake Force Distribution,Electronic Stability Programme,Front Fog Lights,High Level Brake Light,Immobiliser,Mirrors External,Passenger Air Bag,Power-Assisted Steering,Seat - ISOFIX Anchorage Point,Seat Belt Pre-Tensioners,Seat Height Adjustment”,“Speakers,Split-Folding Rear Seats,Tinted Glass”,images/1600/1200/70/0/DOW/18/109/18109357_0_0.3165532943823276_ORIG.jpg,images.com/1600/1200/70/0/DOW/18/109/18109357_1_0.3090620155890714_ORIG.jpg,images.com/1600/1200/70/0/DOW/18/109/18109357_2_0.46076542172304336_ORIG.jpg,images.com/1600/1200/70/0/DOW/18/109/18109357_3_0.2732333010929473_ORIG.jpg,images.com/1600/1200/70/0/DOW/18/109/18109357_4_0.6469716347737734_ORIG.jpg,,,,,,,

The php code I have to explode the data works to a point.
It recognises the first set of " marks in the file but the problem is when it gets to the second set it doesn;t import correctly.

This is the explode statement in the code:

$data = fgets($fh);
	list($data1,$data2) = explode('"',$data,2);
    $data2 = str_replace(array("'", '*', '£', 'é'),array('#', '', '£', 'é'),$data2);
    $data = explode(',',$data1.$data2);

Can anyone help with this please?

Hi sketchgal,

PHP actually has some built-in tools which make parsing CSV files easier:


$file = new SplFileObject("data.csv");
$file->setFlags(SplFileObject::READ_CSV);
$csv = new LimitIterator($file, 1); // Skips first row
foreach ($csv as $row) {
    // do something with $row
}

with your example data, on the first iteration $row would be:


Array
(
    [0] => 18109357
    [1] => TEST
    [2] => Suzuki
    [3] => Alto
    [4] => SZ4
    [5] => 5 Door
    [6] => Hatchback
    [7] => Grey
    [8] => 4 speed
    [9] => Automatic
    [10] => Petrol
    [11] => 996
    [12] => 2013
    [13] => 13
    [14] => 1
    [15] => 7495
    [16] => N
    [17] => 3412
    [18] => 25/06/2013
    [19] => Suzuki Alto 1.0 SZ4 5 Door Hatchback
    [20] =>
    [21] => ABS(Anti-Lock Brakes),Adjustable Steering Column,Air Conditioning,Alloy Wheels,Body Coloured Bumpers,Cloth Interior,Cup Holder,Driver's Air Bag,Electric Windows,Electronic Brake Force Distribution,Electronic Stability Programme,Front Fog Lights,High Level Brake Light,Immobiliser,Mirrors External,Passenger Air Bag,Power-Assisted Steering,Seat - ISOFIX Anchorage Point,Seat Belt Pre-Tensioners,Seat Height Adjustment
    [22] => Speakers,Split-Folding Rear Seats,Tinted Glass
    [23] => images/1600/1200/70/0/DOW/18/109/18109357_0_0.3165532943823276_ORIG.jpg
    [24] => images.com/1600/1200/70/0/DOW/18/109/18109357_1_0.3090620155890714_ORIG.jpg
    [25] => images.com/1600/1200/70/0/DOW/18/109/18109357_2_0.46076542172304336_ORIG.jpg
    [26] => images.com/1600/1200/70/0/DOW/18/109/18109357_3_0.2732333010929473_ORIG.jpg
    [27] => images.com/1600/1200/70/0/DOW/18/109/18109357_4_0.6469716347737734_ORIG.jpg
    [28] =>
    [29] =>
    [30] =>
    [31] =>
    [32] =>
    [33] =>
    [34] =>
)

Brilliant! Thanks for the help fretburner, work’s perfectly now.