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
removeControlLiterals()  : bool|int|float|string|null
Removes control literals from the beginning of a cell value
shallFilterValue()  : bool
Whether cell value shall be filtered.

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

protected static prefixControlLiterals(bool|int|float|string|null $cellValue) : bool|int|float|string|null

(e.g. =+value --> '=+value)

Parameters
$cellValue : bool|int|float|string|null
Return values
bool|int|float|string|null

removeControlLiterals()

Removes control literals from the beginning of a cell value

protected static removeControlLiterals(bool|int|float|string|null $cellValue) : bool|int|float|string|null

(e.g. =+value --> value)

Parameters
$cellValue : bool|int|float|string|null
Return values
bool|int|float|string|null

shallFilterValue()

Whether cell value shall be filtered.

protected static shallFilterValue(bool|int|float|string|null $cellValue) : bool

This applies to everything that is not or cannot be represented as boolean, integer or float.

Parameters
$cellValue : bool|int|float|string|null
Return values
bool

        
On this page

Search results