Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SplFileObject::READ_CSV doesn't appear to be reading CSV file correctly - ignoring enclosure "

I have read documentation and searched this site extensively prior to asking this question.

$file = new SplFileObject("/var/www/qacentre/compliance/compliance.csv");
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {

}

Example CSV line;

"ABEL, TAMMY 454454","End of: ABEL, TAMMY 454454",QP544454,28/10/2012 11:41,"0811 unlawfully use, possess","STEPHENS, JEREMY 54544454",LINK OPERATIONS,Located details incorrect,Entity: FORD FALCON Reg #: Colour: White

I have echoed the output and it appears that it is not recognizing the enclosure character ("). I have attempted to use SetCSVControl although i am using the default enclosure. I have also tried escaping the enclosure character in SetCSVControl (as I have no control over the CSV file I am exporting).

For example when I print the result for the first column, which should be ( ABEL, TAMMY 454454 ) I am receiving ( "ABEL ).

I have read this could be an issue with my locale setting (en-GB,en-US;q=0.8,en;q=0.6)?

Any help would be great.

like image 609
James Holz Avatar asked Nov 04 '22 04:11

James Holz


1 Answers

You could use SplFileObject::fgetcsv

 public array SplFileObject::fgetcsv ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )

It says in the example;

<?php
$file = new SplFileObject("data.csv");
while (!$file->eof()) {
    var_dump($file->fgetcsv());
}
?>

or

<?php
$file = new SplFileObject("animals.csv");
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
    list($animal, $class, $legs) = $row;
    printf("A %s is a %s with %d legs\n", $animal, $class, $legs);
}
?>

NOTE:

%s means strings, %d is an integer

In your specific case

$file = new SplFileObject("/var/www/qacentre/compliance/compliance.csv");
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
var_dump($row);
}

or you could just

<?php

    $file = new SplFileObject("/var/www/qacentre/compliance/compliance.csv");
    while (!$file->eof()) {
        var_dump($file->fgetcsv());
    }
?>
like image 95
Elias Nicolas Avatar answered Nov 15 '22 04:11

Elias Nicolas