I have something like that in my code, which is working well since a few years :
$spl_file = new SplFileObject($file_path, 'r', false, null);
$spl_file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD);
$result = $spl_file->fgetcsv();
I did different tests between PHP 8.0 and PHP 8.1. If we have in our file (e.g. $file_path), a line like "Value1","Val"
or Value1,Val
, the result would be the same in all cases for PHP 8.0 and PHP 8.1, e.g. an array containing Value1
and Val
. This is the expected behavior.
However, if we have a broken csv file, it does not act the same in both PHP versions. I found different cases with different behaviors. Example, for "Value1,Val
, "Value1","Val
and Value1","Val
. PHP 8.0 will return an empty array in each case, but PHP 8.1 will return for the first 2 cases Value1
and Val
, and Value1"
and Val
for the last one.
This is a great improvement, however this update could have an important impact for some existing cases. Is there a way to keep the same behavior for fgetcsv, while updating to 8.1?