PHP code example of tpmanc / csvhelper

1. Go to this page and download the library: Download tpmanc/csvhelper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tpmanc / csvhelper example snippets


use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->delimiter('|')->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->encode('cp1251', 'utf-8')->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->offset(1)->limit(1)->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

use tpmanc\csvhelper\CsvHelper;

...

$lineCount = 0;
$array = [];
CsvHelper::open('files/file.csv')->parse(function($line) use(&$lineCount, &$array) {
    $lineCount++;
    $array[] = $line[0];
});
echo $lineCount;
echo $array[0];
echo $array[1];

use tpmanc\csvhelper\CsvHelper;

...

$file = CsvHelper::create()->delimiter(';');

$file->encode('cp1251', 'utf-8'); // change encoding
$file->addLine('1.;France;'); // add row to file by string
$file->addLine([
    2,
    'Germany'
]); // add row to file by array

$file->save('./new-file.csv');