merlin
merlin

Reputation: 2927

CSV parsing with str_getcsv fails on new line

While reading a csv file with PHP a problem occured with a line break within the CSV file. The contents of one cell will be split once a comma is followed by a line break:

$csv = array_map('str_getcsv', file($file));

first,second,"third,
    more,text","forth"
next,dataset

This will result in:

1) first | second | third
2) more text | forth
3) next | dataset

While it should result in:

1) first | second | third more text | forth
2) next | dataset

Is this a bug within str_getcsv?

Upvotes: 1

Views: 1744

Answers (2)

Harry Bosh
Harry Bosh

Reputation: 3790

<?php

$csvString = "ID,Condition,Condition,Condition,Condition,AdSize,Content:Text,Content:Text,Content:Text,Content:ImageUrl,Content:LandingPageUrl,Archive,Default
ID,Locations:Region,Device Properties:Device,Weather:Condition,Dmp:Liveramp,AdSize,title1,description1,price1,imageUrl1,landingPageUrl1,Archive,Default
ROW_001,\"Wa, Ca, Tn\",Mobile,Snow,12345,300x250,Hello Washingtonian,My Custom Description,10,http://domain/Snow.jpg,https://www.example.com,TRUE,
ROW_002,Wa,Mobile,Snow,12345,300x250,Hello Washingtonian,My Custom Description,10,http://domain/New_Snow.jpg,https://www.example.com,,
ROW_003,Wa,Mobile,,,300x250,Hello Washingtonian,My Custom Description,10,http://domain/clear.jpg,https://www.example.com,,
ROW_004,,,,,300x250,Hello,My Custom Description,20,http://domain/clear.jpg,https://www.example.com,,TRUE";

function csvToArray($csvString, $delimiter = ',', $lineBreak = "\n") {
    $csvArray = [];
    $rows = str_getcsv($csvString, $lineBreak); // Parses the rows. Treats the rows as a CSV with \n as a delimiter
    foreach ($rows as $row) {
        $csvArray[] = str_getcsv($row, $delimiter); // Parses individual rows. Now treats a row as a regular CSV with ',' as a delimiter
    }
    return $csvArray;
}

print_r(csvToArray($csvString));

https://gist.github.com/sul4bh/d392315c7049abd86916e077707bf123

Upvotes: 0

Sammitch
Sammitch

Reputation: 32272

Don't do that, use fgetcsv(). You're having problems because file() doesn't care about the string encapsulation in your file.

$fh = fopen('file.csv', 'r');

while( $line = fgetcsv($fh) ) {
    // do a thing
}

fclose($fh);

https://secure.php.net/manual/en/function.fgetcsv.php

And try not to store all the lines into an array before performing your operations if you can help it. Your system's memory usage will thank you.

Upvotes: 2

Related Questions