Download the PHP package htmlburger/carbon-csv without Composer
On this page you can find all versions of the php package htmlburger/carbon-csv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package carbon-csv
Carbon CSV
Carbon CSV is a PHP library aimed at simplifying CSV parsing.
It provides simple interface to ease mapping columns via a header row, or custom column names.
Installation
Usage
Suppose that you have the following CSV:
First Name | Last Name | Company Name | Address |
---|---|---|---|
Homer | Simpson | Springfield Nuclear Power Plant | 742 Evergreen Terrace, Springfield |
Ned | Flanders | The Leftorium | 744 Evergreen Terrace, Springfield |
Here is how you could iterate through the rows:
Would produce the following output:
Alternatively, you could also provide your own column names:
Would produce the following output:
MacOS encoding
When working with files created on a Mac device, you should set the auto_detect_line_endings
PHP variable to 1
.
Settings
To change the delimiter, enclosure and escape characters for the CSV file, simply pass them as arguments after the file path.
Example:
Methods
Methods for skipping rows or columns work with zero based indexes.
skip_to_row(int $row_index)
To skip to a specific row, simply pass the index of the row.
This will tell the parser to start reading from that row until the end of the file.
Contents before skipping to a specific row:
Contents after skipping to a specific row:
skip_to_column(int $col_index)
To skip to a specific column, simply pass the index of the column.
Contents before skipping to a specific column:
Contents after skipping to a specific column:
skip_columns(array $col_indexes)
To skip multiple columns, pass the indexes of those columns as an array.
Contents before skipping columns:
Contents after skipping columns:
use_first_row_as_header()
To use the first row from the CSV, simply call this method.
Note: if skip_to_row
is called prior to calling use_first_row_as_header
, the parser will use the new first row as a header.
Contents before assigning a header row:
Contents after assigning a header row:
Since we're telling the parser to use the first row as a header row, it is assigned and skipped.
set_column_names(array $columns_mapping)
If you wish to use your own indexes for the columns, pass them using an array.
Note: you can use set_column_names
in conjunction with use_first_row_as_header
, so you can set the names of the columns based on the header row.
Example without use_first_row_as_header
(using a file without a head row):
Contents before setting custom column names:
Contents after setting custom column names:
Example with use_first_row_as_header
(using a file with a head row):
Contents before setting custom column names:
Contents after setting custom column names:
set_encoding($encoding)
Set the encoding of the CSV file. This is needed, so it can be properly converted to utf-8
Example:
count()
Get the total number of rows in the CSV file (this skips the empty rows):
$total_number_of_rows = $csv->count()
is equivalent to count($csv->to_array())
.