SplFileObject + LimitIterator + offset
I have a data file with two lines (two lines just for my example, in reality this file can contain millions of lines) and I am using SplFileObject and LimitIterator with offset message. But in some cases this combination has strange behavior:
$offset = 0;
$file = new \SplFileObject($filePath);
$fileIterator = new \LimitIterator($file, $offset, 100);
foreach ($fileIterator as $key => $line) {
echo $key;
}
Output: 01
But with $ offset set to 1 , the output is empty (foreach doesn't iterate over any string).
My data file contains the following:
{"generatedAt":1434665322,"numRecords":"1}
{"id":"215255","code":"NB000110"}
What am I doing wrong?
thank
source to share
Required:
Use SplFileObject
to process multiple records:
- starting record number
- for a given number of records or up to
EOF
.
The problem is that it SplFileObject
gets confused about the relation last record
in the file. This prevents it from looping correctly foreach
.
This code uses SplFileObject
both 'skip records' and 'process records'. Alas, it cannot use loops foreach
.
- Skip a series of records from the beginning of the file (
$offset
). - Process a specified number of records or concatenate end of file (
$recordsToProccess
)
Code:
<?php
$filePath = __DIR__ . '/Q30932555.txt';
// $filePath = __DIR__ . '/Q30932555_1.txt';
$offset = 1;
$recordsToProcess = 100;
$file = new \SplFileObject($filePath);
// skip the records
$file->seek($offset);
$recordsProcessed = 0;
while ( ($file->valid() || strlen($file->current()) > 0)
&& $recordsProcessed < $recordsToProcess
) {
$recordsProcessed++;
echo '<br />', 'current: ', $file->key(), ' ', $file->current();
$file->next();
}
source to share