CsvReader
extends BaseTableReader
in package
Table of Contents
Properties
- $BOMEncoding : string
- $BOMLength : int
- $FixedWidthFields : array<int, int>
- $IsLoaded : bool
- $LineEnding : string
- $Separator : string
- $StripTags : bool
Methods
- detectSeparator() : string
- This method detects the used separator of the given line, by counting the character (one of ",", ";", "\t" or "|") that is mostly used.
- loadFile() : void
- This loads a .csv file.
- loadFormatFile() : void
- This loads a fixed-width definition of the .csv-file.
- loadFormatString() : void
- This loads a fixed-width definition of the .csv-file.
- loadString() : void
- This loads a csv by it's content.
- setStripTags() : self
- If set to true, html-tags were stripped away from the csv-content
Properties
$BOMEncoding read-only
public
string
$BOMEncoding
the detected BOM encoding (see https://de.wikipedia.org/wiki/Byte_Order_Mark)
$BOMLength read-only
public
int
$BOMLength
the detected BOM length
$FixedWidthFields read-only
public
array<int, int>
$FixedWidthFields
an array of int's that where loaded by the loadFormatFile()-method
$IsLoaded read-only
public
bool
$IsLoaded
returns true, if the data was loaded and ready to read.
$LineEnding read-only
public
string
$LineEnding
the detected lineending (\n => Unix/Linux/MacOS, \r => MacOS <= 9, \r\n => Windows)
$Separator read-only
public
string
$Separator
the detected separator (",", ";", "\t" or "|")
$StripTags read-only
public
bool
$StripTags
Methods
detectSeparator()
This method detects the used separator of the given line, by counting the character (one of ",", ";", "\t" or "|") that is mostly used.
public
static detectSeparator(string $line) : string
<?php
use Locr\Lib\CsvReader;
$separator = CsvReader::detectSeparator('foo;bar;baz');
print $separator; // ;
Parameters
- $line : string
Return values
stringloadFile()
This loads a .csv file.
public
loadFile(string $filename) : void
<?php
use Locr\Lib\CsvReader;
$csvReader = new CsvReader();
$csvReader->loadFile('file.csv');
Parameters
- $filename : string
loadFormatFile()
This loads a fixed-width definition of the .csv-file.
public
loadFormatFile(string $filename[, bool $detectAndSetHeaderFields = false ]) : void
<?php
use Locr\Lib\CsvReader;
$csvReader = new CsvReader();
$csvReader->loadFile('file.csv');
$csvReader->loadFormatFile('file_format.csv');
Parameters
- $filename : string
- $detectAndSetHeaderFields : bool = false
loadFormatString()
This loads a fixed-width definition of the .csv-file.
public
loadFormatString(string $content[, bool $detectAndSetHeaderFields = false ]) : void
<?php
use Locr\Lib\CsvReader;
$csvReader = new CsvReader();
$csvReader->loadFile('file.csv');
$csvReader->loadFormatString('5|10|10|5|3');
// alternative format
$csvFormat = "Fieldname|Length|Start|Stop\n";
$csvFormat .= "id|3|1|3\n";
$csvFormat .= "country|8|4|11\n";
$csvFormat .= "city|13|12|24\n";
$csvFormat .= "postal|7|25|31\n";
$csvFormat .= "street|10|32|41\n";
$csvFormat .= "house|5|42|46";
$csvReader->loadFormatString($csvFormat, true);
Parameters
- $content : string
- $detectAndSetHeaderFields : bool = false
loadString()
This loads a csv by it's content.
public
loadString(string $content) : void
<?php
use Locr\Lib\CsvReader;
$csvReader = new CsvReader();
$csvReader->loadString('foo|bar|baz');
$rows = $csvReader->readDatasets();
print $rows[0][0]; // foo
print $rows[0][1]; // bar
print $rows[0][2]; // baz
Parameters
- $content : string
setStripTags()
If set to true, html-tags were stripped away from the csv-content
public
setStripTags(bool $stripTags) : self
<?php
use Locr\Lib\CsvReader;
$csvReader = new CsvReader();
$csvReader->setStripTags(true);
$csvReader->loadString('foo|bar|<hello>world<hello>');
$rows = $csvReader->readDatasets();
print $rows[0][2]; // world
Parameters
- $stripTags : bool