Download the PHP package sebastian/csv-parser without Composer
On this page you can find all versions of the php package sebastian/csv-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sebastian/csv-parser
More information about sebastian/csv-parser
Files in sebastian/csv-parser
Package csv-parser
Short Description Library for type-safe parsing of CSV files
License BSD-3-Clause
Homepage https://github.com/sebastianbergmann/csv-parser
Informations about the package csv-parser
sebastian/csv-parser
Library for type-safe parsing of CSV files.
Installation
You can add this library as a local, per-project dependency to your project using Composer:
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
Usage
example.csv
The example above shows how to use a Schema
object to configure how a string from a line of comma-separated values is parsed into an array element:
1
refers to the position of the string in the line of comma-separated values'a'
specifies the key for the value in the associative array that is generated for each recordType::integer()
specifies the type that should be used to store the value in the associative array that is generated for each record
The following types are available:
- boolean (
Type::boolean()
; uses(bool)
type cast) - integer (
Type::integer()
; uses(int)
type cast) - float (
Type::float()
; uses(float)
type cast) - string (
Type::string()
) - object (
Type::object($mapper)
;$mapper
is an object that implements theSebastianBergmann\CsvParser\ObjectMapper
interface)
The Parser::parse()
method requires two arguments:
- The first argument,
$filename
, is the path to the CSV file that should be parsed - The second argument,
$schema
, is theSchema
object we discussed above
Running the example shown above prints the output shown below:
The $parser->ignoreFirstLine()
method can be used to configure the parser to ignore the first line of the CSV file.
The $parser->setSeparator()
method can be used to configure the parser to use a separator different from the default ,
.
The $parser->setEnclosure()
method can be used to configure the parser to use an enclosure different from the default "
.
The $parser->setEscape()
method can be used to configure the parser to use an escape different from the default "
.
Please note that this is a low maintenance project.