www.pokeroconnor.com


SPL CSV File Iterator

August1

When using PHP’s SplFileObject for CSV file manipulation, as you will likely discover fgetcsv() also includes empty lines in the returned array.

The suggestion is to use SplFileObject::DROP_NEW_LINE or SplFileObject::SKIP_EMPTY to handle this.

However, I find I often need to use both to achieve the desired result of ignoring blank or empty lines. To achieve this, you need to use a bitmask in your setflags argument.

A value of ’7′ corresponds to both SplFileObject::DROP_NEW_LINE AND SplFileObject::SKIP_EMPTY, e.g.

$file = new SplFileObject($path);
$file->setFlags(7);

setFlags(15) would correspond to a bitmask for DROP_NEWLINE, SKIP_EMPTY, READ_CSV.

//
posted under php

Email will not be published

Website example

Your Comment: