PHP code example of jletrondo / csv

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

    

jletrondo / csv example snippets


namespace Jletrondo\Csv;

use Jletrondo\Csv\Reader;

$reader = new Reader($params);

$results = $reader->read('path/to/your/file.csv');

$this->reader->setCallback(string $method, object $context);

public function custom_validation($row, $index) 
{
    // Custom validations must return an associative array 
    return [
        'status' => false, // true means no errow in the row
        'column_errors' => [] // if row has error, this is the array where error message is stored.
    ];
}



use Jletrondo\Csv\Reader;

class CsvProcessor
{
    private $columns;
    private $reader;

    public function __construct()
    {
        $this->columns = [
            [
                'name'        => 'company', 
                'column_name' => 'company', 
                'type'        => 'string', 
                'validate'    => 'uppercase|max_length[7]'
            ],
            [
                'name'        => 'name', 
                'column_name' => 'fullname',
                'type'        => 'string',
                'validate'    => '       ]);

        // $this->reader->initialize([
        //     'columns' => $this->columns
        // ]);
    }

    public function process()
    {
        try {
            $this->reader->setCallback('custom_validation', $this);
            $result = $this->reader->read(__DIR__ . '/file.csv');
            print_r($result);
        } catch (Exception $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public function custom_validation($row, $index) 
    {
        $errors = [];

        if (empty($row['birth date'])) {
            $errors[] = "Birth Date is 

    use Jletrondo\Csv\Reader;
    $reader = new Reader([
        'columns' => $columns, // your column definitions
    ]);
    // Set a progress callback
    $reader->setProgressCallback(function($processed, $total) {
        if ($total) {
            echo "Progress: " . round($processed / $total 100, 2) . "%\n";
        } else {
            echo "Rows processed: $processed\n";
        }
    });
    // Optionally set other callbacks or options...
    // $reader->setCallback('custom_validation', $yourObject);
    $result = $reader->read('path/to/your.csv');



use Jletrondo\Csv\Reader;

class CsvProcessor
{
    private $columns;
    private $reader;

    public function __construct()
    {
        $this->columns = [
            ['column_name' => 'name', 'name' => 'name', 'type' => 'string', 'validate' => '<CSV
            name,email
            Jane,[email protected]
        CSV;
        $file = tempnam(sys_get_temp_dir(), 'csv_');
        file_put_contents($file, $csv);

        $callback = new class {
            public function exceptionValidation($row, $rowIndex) {
                $exceptionMsg = "";
                $errors = [];
               
                try {
                    if($row['email']) {
                        throw new \Exception('Exception error message');
                    }
                } catch (\Exception $e) {
                    $errors[] = "The row has encountered an internal error. Please check and try again.";
                    $exceptionMsg = $e->getMessage();
                }

                return [
                    'status' => empty($errors),
                    'column_errors' => $errors ?? [],
                    'exception' => $exceptionMsg
                ];
            }
        };

        $this->reader->setCallback('exceptionValidation', $callback);
        $result = $this->reader->read($file);

        print_r($result);
        unlink($file);
    }
}

// Create an instance of CsvProcessor and process the CSV file
$csvProcessor = new CsvProcessor();
$csvProcessor->process();
bash
composer test -- tests/Reader/exceptiontest.php