class Csv extends Reader (View source)

Constants

MODE_COLUMN

SKIP_BLANK

SKIP_HEADER

STRICT

Properties

protected $separator from  Reader
protected $separators
protected $stream from  Reader

Methods

__construct(string $path)

Open file with comma-separated values

__destruct()

No description

from  Reader
array<int, string>
getHeaders()

Get headers from file

from  Reader
resource
getStream()

Get file pointer resource

from  Reader
refresh()

Rewind the position of file pointer and refresh headers

from  Reader
setChunk(int|null $length)

Set maximum line length

from  Reader
setDataTransferObject(string|null $dto)

Set Data Transfer Object class

from  Reader
setEndOfLine(string $eol)

Set end-of-line

from  Reader
int
setFlags(int $flags)

Set the behavior of the fetch() method, and returns the previously defined flags.

from  Reader
callable
setFilter(callable $filter)

Set a custom filter that can be used to skip specific rows by returning false in the callback, or simply to modify the values received by reference.

from  Reader
setLimit(int $count, int $offset = 0)

Set the maximum number of rows returned and skips a certain number of rows before starting to return rows.

from  Reader
array<int, string>|array<string, string>|false
fetch()

Fetch a row from file pointer

from  Reader
converter()

Get a Converter instance.

from  Reader
setEnclosure(string $enclosure)

Set enclosure for read CSV

setProprietaryEscape(string $escape)

Set proprietary escape mechanism for read CSV

setSeparators(array $separators)

Set separators

array<int, string>|false
parse(string $separator, string $entry)

Parse lines

Details

__construct(string $path)

Open file with comma-separated values

Parameters

string $path

Exceptions

Exception

__destruct()

No description

array<int, string> getHeaders()

Get headers from file

Return Value

array<int, string>

resource getStream()

Get file pointer resource

Return Value

resource

refresh()

Rewind the position of file pointer and refresh headers

setChunk(int|null $length)

Set maximum line length

Parameters

int|null $length

Exceptions

Exception

setDataTransferObject(string|null $dto)

Set Data Transfer Object class

Note: To ensure the key format, use setFilter() method.

Parameters

string|null $dto

setEndOfLine(string $eol)

Set end-of-line

Parameters

string $eol

int setFlags(int $flags)

Set the behavior of the fetch() method, and returns the previously defined flags.

Parameters

int $flags

Return Value

int

Exceptions

Exception

callable setFilter(callable $filter)

Set a custom filter that can be used to skip specific rows by returning false in the callback, or simply to modify the values received by reference.

Note: For headers, the filter is only used to modify the values. Note: When defining a callback, the previous callback will be returned.

E.g:

$handle->setFilter(function (array &$fields, $index) {
  if ($index === 0) {
    // Convert header
    $fields = str_to_snake_case($fields);
  } else {
    // Convert rows
    $field = array_map('stripcslashes', $field);
  }
});

Parameters

callable $filter

Return Value

callable

Exceptions

Exception

setLimit(int $count, int $offset = 0)

Set the maximum number of rows returned and skips a certain number of rows before starting to return rows.

Note: If this method is not used, it will be limited to 1000 rows.

Parameters

int $count

Set limit rows (Note: Set 0 for unlimited).

int $offset

Set offset row.

array<int, string>|array<string, string>|false fetch()

Fetch a row from file pointer

Return Value

array<int, string>|array<string, string>|false

Exceptions

Exception

Converter converter()

Get a Converter instance.

Return Value

Converter

setEnclosure(string $enclosure)

Set enclosure for read CSV

Parameters

string $enclosure

Exceptions

Exception

setProprietaryEscape(string $escape)

Set proprietary escape mechanism for read CSV

Parameters

string $escape

Exceptions

Exception

setSeparators(array $separators)

Set separators

Parameters

array $separators

Exceptions

Exception

protected array<int, string>|false parse(string $separator, string $entry)

Parse lines

Parameters

string $separator

Field separator

string $entry

Line content

Return Value

array<int, string>|false