PHP code example of htmlburger / carbon-csv

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

    

htmlburger / carbon-csv example snippets


use \Carbon_CSV\CsvFile;
use \Carbon_CSV\Exception as CsvException;

try {
    $csv = new CsvFile('path-to-file/filename.csv');
    $csv->use_first_row_as_header();

    foreach ($csv as $row) {
        print_r($row);
    }
} catch (CsvException $e) {
    exit("Couldn't parse CSV file: " . $e->getMessage()); 
}

use \Carbon_CSV\CsvFile;
use \Carbon_CSV\Exception as CsvException;

try {
    $csv = new CsvFile('path-to-file/filename.csv');
    $csv->use_first_row_as_header();
    $csv->set_column_names([
        'First Name'   => 'fname',
        'Last Name'    => 'lname',
        'Company Name' => 'company',
        'Address'      => 'address',
    ]);

    foreach ($csv as $row) {
        print_r($row);
    }
} catch (CsvException $e) {
    exit("Couldn't parse CSV file: " . $e->getMessage()); 
}

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv', ';', '|', '/');
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_to_row(1);
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_to_column(2);
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_columns(array(0, 2, 3));
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->use_first_row_as_header();
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename-no-head-rows.csv');
$csv->set_column_names([
    0 => 'first_name',
    1 => 'last_name',
    2 => 'company_name',
    3 => 'address',
]);
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename-no-head-rows.csv');
$csv->use_first_row_as_header();
$csv->set_column_names([
    'First Name' => 'first_name',
    'Last Name' => 'last_name',
    'Company Name' => 'company_name',
    'Address' => 'address',
]);
$rows = $csv->to_array();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->set_encoding('windows-1251');
$total_number_of_rows = $csv->count();

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$total_number_of_rows = $csv->count();

ini_set( 'auto_detect_line_endings', 1 );