CsvUtility

Class with helper functions for CSV handling

Table of Contents

Constants

TYPE_PASSTHROUGH  = 0
whether to passthrough data as is, without any modification
TYPE_PREFIX_CONTROLS  = 2
whether to prefix control characters like `=`, `+`, ... to become `'=`, `'+`, .
TYPE_REMOVE_CONTROLS  = 1
whether to remove control characters like `=`, `+`, .

Methods

csvToArray()  : array<string|int, mixed>
Convert a string, formatted as CSV, into a multidimensional array
csvValues()  : string
Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars.
assertCellValueType()  : void
Asserts scalar or null types for given cell value.
prefixControlLiterals()  : bool|int|float|string|null
Prefixes control literals at the beginning of a cell value with a single quote (e.g. `=+value` --> `'=+value`)
removeControlLiterals()  : bool|int|float|string|null
Removes control literals from the beginning of a cell value (e.g. `=+value` --> `value`)
shallFilterValue()  : bool
Whether cell value shall be filtered, applies to everything that is not or cannot be represented as boolean, integer or float.

Constants

TYPE_PASSTHROUGH

whether to passthrough data as is, without any modification

public mixed TYPE_PASSTHROUGH = 0

TYPE_PREFIX_CONTROLS

whether to prefix control characters like `=`, `+`, ... to become `'=`, `'+`, .

public mixed TYPE_PREFIX_CONTROLS = 2

..

TYPE_REMOVE_CONTROLS

whether to remove control characters like `=`, `+`, .

public mixed TYPE_REMOVE_CONTROLS = 1

..

Methods

csvToArray()

Convert a string, formatted as CSV, into a multidimensional array

public static csvToArray(string $input[, string $fieldDelimiter = ',' ][, string $fieldEnclosure = '"' ][, int $maximumColumns = 0 ]) : array<string|int, mixed>

This cannot be done by str_getcsv, since it's impossible to handle enclosed cells with a line feed in it

Parameters
$input : string

The CSV input

$fieldDelimiter : string = ','

The field delimiter

$fieldEnclosure : string = '"'

The field enclosure

$maximumColumns : int = 0

The maximum amount of columns

Return values
array<string|int, mixed>

csvValues()

Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars.

public static csvValues(array<string|int, string> $row[, string $delim = ',' ][, string $quote = '"' ][, int $type = self::TYPE_REMOVE_CONTROLS ]) : string
Parameters
$row : array<string|int, string>

Input array of values

$delim : string = ','

Delimited, default is comma

$quote : string = '"'

Quote-character to wrap around the values.

$type : int = self::TYPE_REMOVE_CONTROLS

Output behaviour concerning potentially harmful control literals

Return values
string

A single line of CSV

assertCellValueType()

Asserts scalar or null types for given cell value.

protected static assertCellValueType(mixed $cellValue) : void
Parameters
$cellValue : mixed

prefixControlLiterals()

Prefixes control literals at the beginning of a cell value with a single quote (e.g. `=+value` --> `'=+value`)

protected static prefixControlLiterals(mixed $cellValue) : bool|int|float|string|null
Parameters
$cellValue : mixed
Return values
bool|int|float|string|null

removeControlLiterals()

Removes control literals from the beginning of a cell value (e.g. `=+value` --> `value`)

protected static removeControlLiterals(mixed $cellValue) : bool|int|float|string|null
Parameters
$cellValue : mixed
Return values
bool|int|float|string|null

shallFilterValue()

Whether cell value shall be filtered, applies to everything that is not or cannot be represented as boolean, integer or float.

protected static shallFilterValue(mixed $cellValue) : bool
Parameters
$cellValue : mixed
Return values
bool

        
On this page

Search results