PHP code example of mmerian / csv

1. Go to this page and download the library: Download mmerian/csv 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/ */

    

mmerian / csv example snippets



use Csv\Reader;

$reader = new Reader($file, array(
    'hasHeader' => true,
    'inputEncoding' => 'ISO-8859-15',
    'outputEncoding' => 'UTF-8',
    'delimiter' => ';'
));

foreach ($reader as $line) {
    /*
     * $line is an array.
     * If the CSV file has an header,
     * the array keys are the header fields
     */
    echo $line['first_name'];
}

// Or

while ($line = $reader->fetch()) {
    // Process line
}


use Csv\Reader;

$reader = new Reader($file, array(
    'hasHeader' => true,
    'inputEncoding' => 'ISO-8859-15',
    'outputEncoding' => 'UTF-8',
    'delimiter' => ';'
));
$reader->registerFormatter('birthdate', function($date) {
    return preg_replace('/^([0-9]+)-([0-9]+)-([0-9]+)$/', '$3/$2/$1', $date);
});

foreach ($reader as $line) {
    echo $line['birthdate']; // will echo 12/05/1972 for the first line
}