This PHP library is an opinionated wrapper around PHP's fgetcsv()
function
trying to be suitable for guessing (column and row) separators and file encoding
as generated by typical spreadsheet applications (looking at you, Excel).
The heuristics used to guess the encoding detects Unicode encodings correctly.
For legacy code pages, Western European encodings are detected.
Using this library for non-commercial or free software projects is permissible in accordance with the GNU General Public License (version 3 or later).
The whole library can be compiled into a single PHP file which can be require
d
from any PHP module:
$ make
$ php <<EOF
<?php
require 'csvreader.php';
// ...
EOF
In your project's composer.json
add the following configuration options and
run composer install
:
{
"repositories": [
{
"type": "github",
"url": "https://github.com/riiengineering/php-csvreader"
}
],
"require": {
"riiengineering/php-csvreader": "dev-main"
}
}
<?php
require_once 'csvreader.php'; /* if using the classic method */
require_once 'vendor/autoload.php'; /* if using composer */
$reader = new \riiengineering\csvreader\CSVReader(
$file,
array(
'id',
'product',
'currency',
'price',
),
array(
'separator' => ';',
'encoding' => 'AUTO',
'line-separator' => 'AUTO',
));
foreach ($reader as $row) {
var_dump($row) . PHP_EOL;
}