Skip to content

Latest commit

 

History

History
77 lines (59 loc) · 1.6 KB

README.md

File metadata and controls

77 lines (59 loc) · 1.6 KB

php-csvreader

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).

Installation

Classic

The whole library can be compiled into a single PHP file which can be required from any PHP module:

$ make
$ php <<EOF
<?php
require 'csvreader.php';

// ...
EOF

Composer

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"
	}
}

Usage

<?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;
}

riiengineered.