Csv
extends BaseReader
in package
Table of Contents
Constants
- DEFAULT_FALLBACK_ENCODING = 'CP1252'
- GUESS_ENCODING = 'guess'
- UTF16BE_BOM = "\xfe\xff"
- UTF16BE_BOM_LEN = 2
- UTF16BE_LF = "\x00\n"
- UTF16LE_BOM = "\xff\xfe"
- UTF16LE_BOM_LEN = 2
- UTF16LE_LF = "\n\x00"
- UTF32BE_BOM = "\x00\x00\xfe\xff"
- UTF32BE_BOM_LEN = 4
- UTF32BE_LF = "\x00\x00\x00\n"
- UTF32LE_BOM = "\xff\xfe\x00\x00"
- UTF32LE_BOM_LEN = 4
- UTF32LE_LF = "\n\x00\x00\x00"
- UTF8_BOM = ""
- UTF8_BOM_LEN = 3
Properties
- $castFormattedNumberToNumeric : bool
- $fileHandle : resource
- $ignoreRowsWithNoCells : bool
- Ignore rows with no cells? Identifies whether the Reader should ignore rows with no cells.
- $includeCharts : bool
- Read charts that are defined in the workbook? Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
- $loadSheetsOnly : null|array<string|int, string>
- Restrict which sheets should be loaded? This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
- $preserveNumericFormatting : bool
- $readDataOnly : bool
- Read data only? Identifies whether the Reader should only read data values for cells, and ignore any formatting information; or whether it should read both data and formatting.
- $readEmptyCells : bool
- Read empty cells? Identifies whether the Reader should read data values for all cells, or should ignore cells containing null value or empty string.
- $readFilter : IReadFilter
- IReadFilter instance.
- $securityScanner : XmlScanner|null
- $valueBinder : IValueBinder|null
- $constructorCallback : callable|null
- Callback for setting defaults in construction.
- $contiguous : bool
- Load rows contiguously.
- $decimalSeparator : string
- $defaultEscapeCharacter : string
- The character that will be supplied to fgetcsv when escapeCharacter is null.
- $delimiter : string|null
- Delimiter.
- $enclosure : string
- Enclosure.
- $escapeCharacter : string|null
- The character that can escape the enclosure.
- $fallbackEncoding : string
- Fallback encoding if guess strikes out.
- $getFalse : string
- $getTrue : string
- $inputEncoding : string
- Input encoding.
- $preserveNullString : bool
- $sheetIndex : int
- Sheet index to read.
- $sheetNameIsFileName : bool
- $testAutodetect : bool
- Attempt autodetect line endings (deprecated after PHP8.1)?
- $thousandsSeparator : string
Methods
- __construct() : mixed
- Create a new CSV Reader instance.
- affectedByPhp9() : bool
- canRead() : bool
- Can the current IReader read the file?
- castFormattedNumberToNumeric() : void
- getConstructorCallback() : callable|null
- getContiguous() : bool
- getDelimiter() : string|null
- getEnclosure() : string
- getEscapeCharacter() : string
- getFallbackEncoding() : string
- getIgnoreRowsWithNoCells() : bool
- getIncludeCharts() : bool
- Read charts in workbook? If this is true, then the Reader will include any charts that exist in the workbook.
- getInputEncoding() : string
- getLoadSheetsOnly() : array<string|int, mixed>|null
- Get which sheets to load Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null indicating that all worksheets in the workbook should be loaded.
- getPreserveNullString() : bool
- getReadDataOnly() : bool
- Read data only? If this is true, then the Reader will only read data values for cells, it will not read any formatting or structural information (like merges).
- getReadEmptyCells() : bool
- Read empty cells? If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
- getReadFilter() : IReadFilter
- Read filter.
- getSecurityScanner() : XmlScanner|null
- getSecurityScannerOrThrow() : XmlScanner
- getSheetIndex() : int
- getValueBinder() : IValueBinder|null
- guessEncoding() : string
- guessEncodingBom() : string
- listWorksheetInfo() : array<string|int, mixed>
- Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
- listWorksheetNames() : array<string|int, mixed>
- Returns names of the worksheets from a file, possibly without parsing the whole file to a Spreadsheet object.
- load() : Spreadsheet
- Loads Spreadsheet from file.
- loadIntoExisting() : Spreadsheet
- Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
- loadSpreadsheetFromString() : Spreadsheet
- Loads Spreadsheet from string.
- setConstructorCallback() : void
- Set a callback to change the defaults.
- setContiguous() : self
- setDelimiter() : self
- setEnclosure() : self
- setEscapeCharacter() : self
- Php9 intends to drop support for this parameter in fgetcsv.
- setFallbackEncoding() : self
- setIgnoreRowsWithNoCells() : self
- setIncludeCharts() : $this
- Set read charts in workbook Set to true, to advise the Reader to include any charts that exist in the workbook.
- setInputEncoding() : self
- setLoadAllSheets() : $this
- Set all sheets to load Tells the Reader to load all worksheets from the workbook.
- setLoadSheetsOnly() : $this
- Set which sheets to load.
- setPreserveNullString() : self
- setReadDataOnly() : $this
- Set read data only Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting or structural information (like merges).
- setReadEmptyCells() : $this
- Set read empty cells Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
- setReadFilter() : $this
- Set read filter.
- setSheetIndex() : self
- setSheetNameIsFileName() : self
- setTestAutoDetect() : self
- setValueBinder() : self
- checkSeparator() : void
- Identify any separator that is explicitly set in the file.
- inferSeparator() : void
- Infer the separator if it isn't explicitly set in the file or specified by the user.
- loadSpreadsheetFromFile() : Spreadsheet
- Loads Spreadsheet from file.
- openFile() : void
- Open file for reading.
- processFlags() : void
- skipBOM() : void
- Move filepointer past any BOM marker.
- convertBoolean() : void
- Convert string true/false to boolean, and null to null-string.
- convertFormattedNumber() : string
- Convert numeric strings to int or float values.
- getCsv() : array<int, string|null>|false
- Php8.4 deprecates use of anything other than null string as escape Character.
- guessEncodingNoBom() : string
- guessEncodingTestBom() : void
- guessEncodingTestNoBom() : void
- loadStringOrFile() : Spreadsheet
- Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
- loadStringOrFile2() : void
- openDataUri() : void
- Open data uri for reading.
- openFileOrMemory() : void
- setAutoDetect() : string|null
Constants
DEFAULT_FALLBACK_ENCODING
public
mixed
DEFAULT_FALLBACK_ENCODING
= 'CP1252'
GUESS_ENCODING
public
mixed
GUESS_ENCODING
= 'guess'
UTF16BE_BOM
public
mixed
UTF16BE_BOM
= "\xfe\xff"
UTF16BE_BOM_LEN
public
mixed
UTF16BE_BOM_LEN
= 2
UTF16BE_LF
public
mixed
UTF16BE_LF
= "\x00\n"
UTF16LE_BOM
public
mixed
UTF16LE_BOM
= "\xff\xfe"
UTF16LE_BOM_LEN
public
mixed
UTF16LE_BOM_LEN
= 2
UTF16LE_LF
public
mixed
UTF16LE_LF
= "\n\x00"
UTF32BE_BOM
public
mixed
UTF32BE_BOM
= "\x00\x00\xfe\xff"
UTF32BE_BOM_LEN
public
mixed
UTF32BE_BOM_LEN
= 4
UTF32BE_LF
public
mixed
UTF32BE_LF
= "\x00\x00\x00\n"
UTF32LE_BOM
public
mixed
UTF32LE_BOM
= "\xff\xfe\x00\x00"
UTF32LE_BOM_LEN
public
mixed
UTF32LE_BOM_LEN
= 4
UTF32LE_LF
public
mixed
UTF32LE_LF
= "\n\x00\x00\x00"
UTF8_BOM
public
mixed
UTF8_BOM
= ""
UTF8_BOM_LEN
public
mixed
UTF8_BOM_LEN
= 3
Properties
$castFormattedNumberToNumeric
protected
bool
$castFormattedNumberToNumeric
= false
$fileHandle
protected
resource
$fileHandle
$ignoreRowsWithNoCells
Ignore rows with no cells? Identifies whether the Reader should ignore rows with no cells.
protected
bool
$ignoreRowsWithNoCells
= false
Currently implemented only for Xlsx.
$includeCharts
Read charts that are defined in the workbook? Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
protected
bool
$includeCharts
= false
$loadSheetsOnly
Restrict which sheets should be loaded? This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
protected
null|array<string|int, string>
$loadSheetsOnly
= null
This property is ignored for Csv, Html, and Slk.
$preserveNumericFormatting
protected
bool
$preserveNumericFormatting
= false
$readDataOnly
Read data only? Identifies whether the Reader should only read data values for cells, and ignore any formatting information; or whether it should read both data and formatting.
protected
bool
$readDataOnly
= false
$readEmptyCells
Read empty cells? Identifies whether the Reader should read data values for all cells, or should ignore cells containing null value or empty string.
protected
bool
$readEmptyCells
= true
$readFilter
IReadFilter instance.
protected
IReadFilter
$readFilter
$securityScanner
protected
XmlScanner|null
$securityScanner
= null
$valueBinder
protected
IValueBinder|null
$valueBinder
= null
$constructorCallback
Callback for setting defaults in construction.
private
static callable|null
$constructorCallback
$contiguous
Load rows contiguously.
private
bool
$contiguous
= false
$decimalSeparator
private
string
$decimalSeparator
= '.'
$defaultEscapeCharacter
The character that will be supplied to fgetcsv when escapeCharacter is null.
private
static string
$defaultEscapeCharacter
= (PHP_VERSION_ID < 90000) ? '\\' : ''
It is anticipated that it will conditionally be set to null-string for Php9 and above.
$delimiter
Delimiter.
private
string|null
$delimiter
= null
$enclosure
Enclosure.
private
string
$enclosure
= '"'
$escapeCharacter
The character that can escape the enclosure.
private
string|null
$escapeCharacter
= null
This will probably become unsupported in Php 9. Not yet ready to mark deprecated in order to give users a migration path.
$fallbackEncoding
Fallback encoding if guess strikes out.
private
string
$fallbackEncoding
= self::DEFAULT_FALLBACK_ENCODING
$getFalse
private
string
$getFalse
= 'false'
$getTrue
private
string
$getTrue
= 'true'
$inputEncoding
Input encoding.
private
string
$inputEncoding
= 'UTF-8'
$preserveNullString
private
bool
$preserveNullString
= false
$sheetIndex
Sheet index to read.
private
int
$sheetIndex
= 0
$sheetNameIsFileName
private
bool
$sheetNameIsFileName
= false
$testAutodetect
Attempt autodetect line endings (deprecated after PHP8.1)?
private
bool
$testAutodetect
= true
$thousandsSeparator
private
string
$thousandsSeparator
= ','
Methods
__construct()
Create a new CSV Reader instance.
public
__construct() : mixed
affectedByPhp9()
public
static affectedByPhp9(string $filename[, string $inputEncoding = 'UTF-8' ][, string|null $delimiter = null ][, string $enclosure = '"' ][, string $escapeCharacter = '\' ]) : bool
Parameters
- $filename : string
- $inputEncoding : string = 'UTF-8'
- $delimiter : string|null = null
- $enclosure : string = '"'
- $escapeCharacter : string = '\'
Return values
boolcanRead()
Can the current IReader read the file?
public
canRead(string $filename) : bool
Parameters
- $filename : string
Return values
boolcastFormattedNumberToNumeric()
public
castFormattedNumberToNumeric(bool $castFormattedNumberToNumeric[, bool $preserveNumericFormatting = false ]) : void
Parameters
- $castFormattedNumberToNumeric : bool
- $preserveNumericFormatting : bool = false
getConstructorCallback()
public
static getConstructorCallback() : callable|null
Return values
callable|nullgetContiguous()
public
getContiguous() : bool
Return values
boolgetDelimiter()
public
getDelimiter() : string|null
Return values
string|nullgetEnclosure()
public
getEnclosure() : string
Return values
stringgetEscapeCharacter()
public
getEscapeCharacter() : string
Return values
stringgetFallbackEncoding()
public
getFallbackEncoding() : string
Return values
stringgetIgnoreRowsWithNoCells()
public
getIgnoreRowsWithNoCells() : bool
Return values
boolgetIncludeCharts()
Read charts in workbook? If this is true, then the Reader will include any charts that exist in the workbook.
public
getIncludeCharts() : bool
Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value. If false (the default) it will ignore any charts defined in the workbook file.
Return values
boolgetInputEncoding()
public
getInputEncoding() : string
Return values
stringgetLoadSheetsOnly()
Get which sheets to load Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null indicating that all worksheets in the workbook should be loaded.
public
getLoadSheetsOnly() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|nullgetPreserveNullString()
public
getPreserveNullString() : bool
Return values
boolgetReadDataOnly()
Read data only? If this is true, then the Reader will only read data values for cells, it will not read any formatting or structural information (like merges).
public
getReadDataOnly() : bool
If false (the default) it will read data and formatting.
Return values
boolgetReadEmptyCells()
Read empty cells? If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
public
getReadEmptyCells() : bool
If false it will not read data for cells containing a null value or an empty string.
Return values
boolgetReadFilter()
Read filter.
public
getReadFilter() : IReadFilter
Return values
IReadFiltergetSecurityScanner()
public
getSecurityScanner() : XmlScanner|null
Return values
XmlScanner|nullgetSecurityScannerOrThrow()
public
getSecurityScannerOrThrow() : XmlScanner
Return values
XmlScannergetSheetIndex()
public
getSheetIndex() : int
Return values
intgetValueBinder()
public
getValueBinder() : IValueBinder|null
Return values
IValueBinder|nullguessEncoding()
public
static guessEncoding(string $filename[, string $dflt = self::DEFAULT_FALLBACK_ENCODING ]) : string
Parameters
- $filename : string
- $dflt : string = self::DEFAULT_FALLBACK_ENCODING
Return values
stringguessEncodingBom()
public
static guessEncodingBom(string $filename[, string|null $convertString = null ]) : string
Parameters
- $filename : string
- $convertString : string|null = null
Return values
stringlistWorksheetInfo()
Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
public
listWorksheetInfo(string $filename) : array<string|int, mixed>
Parameters
- $filename : string
Return values
array<string|int, mixed>listWorksheetNames()
Returns names of the worksheets from a file, possibly without parsing the whole file to a Spreadsheet object.
public
listWorksheetNames(string $filename) : array<string|int, mixed>
Readers will often have a more efficient method with which they can override this method.
Parameters
- $filename : string
Return values
array<string|int, mixed>load()
Loads Spreadsheet from file.
public
load(string $filename[, int $flags = 0 ]) : Spreadsheet
Parameters
- $filename : string
-
The name of the file to load
- $flags : int = 0
-
the optional second parameter flags may be used to identify specific elements that should be loaded, but which won't be loaded by default, using these values: IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file
Return values
SpreadsheetloadIntoExisting()
Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
public
loadIntoExisting(string $filename, Spreadsheet $spreadsheet) : Spreadsheet
Parameters
- $filename : string
- $spreadsheet : Spreadsheet
Return values
SpreadsheetloadSpreadsheetFromString()
Loads Spreadsheet from string.
public
loadSpreadsheetFromString(string $contents) : Spreadsheet
Parameters
- $contents : string
Return values
SpreadsheetsetConstructorCallback()
Set a callback to change the defaults.
public
static setConstructorCallback(callable|null $callback) : void
The callback must accept the Csv Reader object as the first parameter, and it should return void.
Parameters
- $callback : callable|null
setContiguous()
public
setContiguous(bool $contiguous) : self
Parameters
- $contiguous : bool
Return values
selfsetDelimiter()
public
setDelimiter(string|null $delimiter) : self
Parameters
- $delimiter : string|null
Return values
selfsetEnclosure()
public
setEnclosure(string $enclosure) : self
Parameters
- $enclosure : string
Return values
selfsetEscapeCharacter()
Php9 intends to drop support for this parameter in fgetcsv.
public
setEscapeCharacter(string $escapeCharacter) : self
Not yet ready to mark deprecated in order to give users a migration path.
Parameters
- $escapeCharacter : string
Return values
selfsetFallbackEncoding()
public
setFallbackEncoding(string $fallbackEncoding) : self
Parameters
- $fallbackEncoding : string
Return values
selfsetIgnoreRowsWithNoCells()
public
setIgnoreRowsWithNoCells(bool $ignoreRowsWithNoCells) : self
Parameters
- $ignoreRowsWithNoCells : bool
Return values
selfsetIncludeCharts()
Set read charts in workbook Set to true, to advise the Reader to include any charts that exist in the workbook.
public
setIncludeCharts(bool $includeCharts) : $this
Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value. Set to false (the default) to discard charts.
Parameters
- $includeCharts : bool
Return values
$thissetInputEncoding()
public
setInputEncoding(string $encoding) : self
Parameters
- $encoding : string
Return values
selfsetLoadAllSheets()
Set all sheets to load Tells the Reader to load all worksheets from the workbook.
public
setLoadAllSheets() : $this
Return values
$thissetLoadSheetsOnly()
Set which sheets to load.
public
setLoadSheetsOnly(string|array<string|int, mixed>|null $sheetList) : $this
Parameters
- $sheetList : string|array<string|int, mixed>|null
Return values
$thissetPreserveNullString()
public
setPreserveNullString(bool $value) : self
Parameters
- $value : bool
Return values
selfsetReadDataOnly()
Set read data only Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting or structural information (like merges).
public
setReadDataOnly(bool $readCellValuesOnly) : $this
Set to false (the default) to advise the Reader to read both data and formatting for cells.
Parameters
- $readCellValuesOnly : bool
Return values
$thissetReadEmptyCells()
Set read empty cells Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
public
setReadEmptyCells(bool $readEmptyCells) : $this
Set to false to advise the Reader to ignore cells containing a null value or an empty string.
Parameters
- $readEmptyCells : bool
Return values
$thissetReadFilter()
Set read filter.
public
setReadFilter(IReadFilter $readFilter) : $this
Parameters
- $readFilter : IReadFilter
Return values
$thissetSheetIndex()
public
setSheetIndex(int $indexValue) : self
Parameters
- $indexValue : int
Return values
selfsetSheetNameIsFileName()
public
setSheetNameIsFileName(bool $sheetNameIsFileName) : self
Parameters
- $sheetNameIsFileName : bool
Return values
selfsetTestAutoDetect()
public
setTestAutoDetect(bool $value) : self
Parameters
- $value : bool
Return values
selfsetValueBinder()
public
setValueBinder(IValueBinder|null $valueBinder) : self
Parameters
- $valueBinder : IValueBinder|null
Return values
selfcheckSeparator()
Identify any separator that is explicitly set in the file.
protected
checkSeparator() : void
inferSeparator()
Infer the separator if it isn't explicitly set in the file or specified by the user.
protected
inferSeparator() : void
loadSpreadsheetFromFile()
Loads Spreadsheet from file.
protected
loadSpreadsheetFromFile(string $filename) : Spreadsheet
Parameters
- $filename : string
Return values
SpreadsheetopenFile()
Open file for reading.
protected
openFile(string $filename) : void
Parameters
- $filename : string
processFlags()
protected
processFlags(int $flags) : void
Parameters
- $flags : int
skipBOM()
Move filepointer past any BOM marker.
protected
skipBOM() : void
convertBoolean()
Convert string true/false to boolean, and null to null-string.
private
convertBoolean(mixed &$rowDatum) : void
Parameters
- $rowDatum : mixed
convertFormattedNumber()
Convert numeric strings to int or float values.
private
convertFormattedNumber(mixed &$rowDatum) : string
Parameters
- $rowDatum : mixed
Return values
stringgetCsv()
Php8.4 deprecates use of anything other than null string as escape Character.
private
static getCsv(resource $stream[, null|int<0, max> $length = null ][, string $separator = ',' ][, string $enclosure = '"' ][, string|null $escape = null ]) : array<int, string|null>|false
Parameters
- $stream : resource
- $length : null|int<0, max> = null
- $separator : string = ','
- $enclosure : string = '"'
- $escape : string|null = null
Return values
array<int, string|null>|falseguessEncodingNoBom()
private
static guessEncodingNoBom(string $filename) : string
Parameters
- $filename : string
Return values
stringguessEncodingTestBom()
private
static guessEncodingTestBom(string &$encoding, string $first4, string $compare, string $setEncoding) : void
Parameters
- $encoding : string
- $first4 : string
- $compare : string
- $setEncoding : string
guessEncodingTestNoBom()
private
static guessEncodingTestNoBom(string &$encoding, string &$contents, string $compare, string $setEncoding) : void
Parameters
- $encoding : string
- $contents : string
- $compare : string
- $setEncoding : string
loadStringOrFile()
Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
private
loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bool $dataUri) : Spreadsheet
Parameters
- $filename : string
- $spreadsheet : Spreadsheet
- $dataUri : bool
Return values
SpreadsheetloadStringOrFile2()
private
loadStringOrFile2(string $filename, Spreadsheet $spreadsheet, bool $dataUri) : void
Parameters
- $filename : string
- $spreadsheet : Spreadsheet
- $dataUri : bool
openDataUri()
Open data uri for reading.
private
openDataUri(string $filename) : void
Parameters
- $filename : string
openFileOrMemory()
private
openFileOrMemory(string $filename) : void
Parameters
- $filename : string
setAutoDetect()
private
setAutoDetect(string|null $value) : string|null
Parameters
- $value : string|null