0

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?

  • 1
    If you're looking to guarantee certain behaviour when handling broken data you're probably going to have to write your own parser. The real solution here is to fix whatever is creating the broken CSV files. – Tangentially Perpendicular Oct 21 '22 at 16:02
  • @TangentiallyPerpendicular We have thousands of clients uploading their own files, we can't just fix "broken CSV files". In some possible problematic cases, those files can have headers that are skipped, because they don't have a valid csv row. Now, they could become "validated", when they should not. – Réal Thibeault Oct 21 '22 at 17:08
  • I wrote "those files can have headers that are skipped, because they don't have a valid csv row" - as an example, it can be a header containing generic info about the source, date, etc of the file source. – Réal Thibeault Oct 21 '22 at 17:17
  • @RéalThibeault, I think this is the [bug](https://bugs.php.net/bug.php?id=66588) that brought about the change. I'd highly doubt that PHP would revert this but if you really wanted to push it, that might be the place to do it. – Chris Haas Oct 21 '22 at 19:18

0 Answers0