Download the PHP package wilgucki/php-csv without Composer
On this page you can find all versions of the php package wilgucki/php-csv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-csv
PHP-CSV
PHP-CSV is a package that allows you to manage CSV files in an object-oriented way.
Installation
There's not much to do, just run this composer command
Usage
Both, Reader and Writer classes are easy to use. In fact the classes are just a wrappers for built-in PHP functions.
Reader
Reader class will help you with reading existing CSV files (no surprise here).
To read CSV file you need to open it first.
Constructor accepts optional arguments you can use to describe your CSV file. The arguments are:
$delimiter
$enclosure
$escape
$encodingFrom
$encodingTo
First three arguments are exactly the same as fgetcsv function arguments described in the manual - http://php.net/manual/en/function.fgetcsv.php
Other two helps you define encoding - $encodingFrom
is the encoding of CSV file and $encodingTo
is encoding we get after file is read.
To read data from the CSV file we can use two functions: readLine
and readAll
. The former will read and return current row while the latter will
read whole CSV file and return it as an array of arrays (each line will be represented by an array).
If the CSV file has a header row, you can use getHeader
function. This function will take the first row from CSV file and use it to set array keys
for readLine
and readAll
functions. This means that instead of numeric keys you can use labels defined in the first row.
Converters
Sometimes data available in csv file need to be converterd into more suitable format, e.g. convert dates into Carbon objects. Converters make this task much easier. All you need to do is to create converter object and specify the column you want to convert.
You can assigon only one converter per column.
Examples
CSV file example
Don't forget to close CSV file after you are done.
Writer
For creating/updating CSV files you can use Writer class. If you want to create file, you need to provide only a writable path.
For updating existing file, you have to use optional $mode
argument. All available modes are described in the manual
(https://secure.php.net/manual/en/function.fopen.php) but only 'w' and 'a' values are usable in this case.
Writer
contructor accepts the same arguments as the Reader
constructor. Only difference is that $encodingFrom
refers to the input encoding and the
$encodingTo
refers to CSV file encoding.
There are two ways of writing CSV files - writeLine
and writeAll
. First method will write a single line to a CSV file, while the second method will
write multiple lines. If for some reason you need to access data you have written to a CSV file you can use flush
method.
Examples
Don't forget to close CSV file after you are done.
TODO
- processors - process csv data and return the results