file = new \SplFileObject( $path, 'rb' ); $this->delimiter = $delimiter; } /** * Position at a byte offset (a record boundary). * * @param int $offset Byte offset. * @return void */ public function seek( int $offset ): void { $this->file->fseek( $offset ); } /** * Read the next record. * * @return array|null {cells: array (empty for a blank line or a record of empty cells), off: start offset, next: offset after the record, blank: bool}; null at the end of the file. */ public function read(): ?array { $start = $this->file->ftell(); if ( $this->file->eof() ) { return null; } $cells = $this->file->fgetcsv( $this->delimiter, '"', '' ); $next = $this->file->ftell(); if ( $next === $start ) { return null; } // A record of empty cells only (Excel writes ";;;" for formatted empty rows) is blank like an empty line. $blank = ( false === $cells || array( null ) === $cells || '' === trim( implode( '', $cells ) ) ); return array( 'cells' => $blank ? array() : $cells, 'off' => $start, 'next' => $next, 'blank' => $blank, ); } /** * Read the record that starts at an offset. * * @param int $offset Offset. * @return array|null Cells or null. */ public function at( int $offset ): ?array { $this->seek( $offset ); $rec = $this->read(); return $rec && ! $rec['blank'] ? $rec['cells'] : null; } /** * Remove the formula-protection apostrophe CsvSafe adds to exported cells. * * @param array $cells Cells. * @return array */ public static function unprotect( array $cells ): array { foreach ( $cells as $i => $cell ) { if ( is_string( $cell ) && strlen( $cell ) > 1 && "'" === $cell[0] && false !== strpos( "=+-@\t\r'", $cell[1] ) ) { $cells[ $i ] = substr( $cell, 1 ); } } return $cells; } }