Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SplFileObject::fgetcsv does not return null for last line in CSV mode #5823

Open
jjanvier opened this issue Jul 30, 2015 · 2 comments
Open

Comments

@jjanvier
Copy link

I'm reading CSV files using SplFileObject class and fgetcsv method. Below code does not work as intended in HHVM 3.8.0 meaning fgetcsv does return false on the LAST line. In PHP 5.6.7-1 the last line returns null.

With the following CSV file

row11;;row13
row21;row22;row23

and the following code

$aFile = new SplFileObject ($aPath);
$aFile->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::READ_CSV);
$aFile->setCsvControl(';');

for($i = 0; $i < 3; $i++) {
    $row = $aFile->fgetcsv();
    var_dump($row);
}

Result for PHP

array(3) {
  [0] =>
  string(5) "row11"
  [1] =>
  string(0) ""
  [2] =>
  string(5) "row13"
}
array(3) {
  [0] =>
  string(5) "row21"
  [1] =>
  string(5) "row22"
  [2] =>
  string(5) "row23"
}
NULL

Result for HHVM

array(3) {
  [0]=>
  string(5) "row11"
  [1]=>
  string(0) ""
  [2]=>
  string(5) "row13"
}
array(3) {
  [0]=>
  string(5) "row21"
  [1]=>
  string(5) "row22"
  [2]=>
  string(5) "row23"
}
bool(false)
@jjanvier jjanvier changed the title SplFileObject::fgetcsv does not skip last line in CSV mode SplFileObject::fgetcsv does not return null for last line in CSV mode Jul 30, 2015
@SiebelsTim
Copy link
Contributor

DROP_NEW_LINE and SKIP_EMPTY produce this result. We match php when you omit both of these flags.

@nyamsprod
Copy link

Hope this little snippet will help you fix this PHP incompatibility

https://3v4l.org/5ZFKe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants