CSV Parsing using ParseCsv PHP class

Parse Csv

What is ParseCsv?

ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly.

Where do I get ParseCsv?

CSV data parser for PHP can be aquired at the ParseCsv GitHub repository

https://github.com/parsecsv/parsecsv-for-php

Do I need to use ParseCsv?

You may not need a library at all. Before using ParseCsv, try using PHP's own functions to see if they meet your needs.

Installation

Installation is easy using Composer. Just run the following on the command line:

composer require parsecsv/php-parsecsv

You may have to manually include Composer's autoloader file in your PHP script:

require_once __DIR__ . '/vendor/autoload.php';

 

Without composer

Not recommended, but technically possible: you can also clone the repository or extract the ZIP. To use ParseCSV, you then have to add

require 'parsecsv.lib.php';

Example Usage

General

$csv = new ParseCsvCsv('data.csv');
print_r($csv->data);

Tab delimited, and encoding conversion

$csv = new ParseCsvCsv();
$csv->encoding('UTF-16', 'UTF-8');
$csv->delimiter = "	";
$csv->parse('data.tsv');
print_r($csv->data);

Auto-detect delimiter character

$csv = new ParseCsvCsv();
$csv->auto('data.csv');
print_r($csv->data);

Parse data with offset

  • ignoring the first X (e.g. two) rows
$csv = new ParseCsvCsv();
$csv->offset = 2;
$csv->parse('data.csv');
print_r($csv->data);

Features

  • ParseCsv is a complete and fully featured CSV solution for PHP
  • Supports enclosed values, enclosed commas, double quotes and new lines.
  • Automatic delimiter character detection.
  • Sort data by specific fields/columns.
  • Easy data manipulation.
  • Basic SQL-like conditionsoffset and limit options for filtering data.
  • Error detection for incorrectly formatted input. It attempts to be intelligent, but can not be trusted 100% due to the structure of CSV, and how different programs like Excel for example outputs CSV data.
  • Support for character encoding conversion using PHP's iconv() and mb_convert_encoding() functions.
  • Supports PHP 5.5 and higher. It certainly works with PHP 7.2 and all versions in between.

Source: https://github.com/parsecsv/parsecsv-for-php

 

Visit sunny St. George, Utah, USA