1. Go to this page and download the library: Download inwebo/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/ */
inwebo / csv example snippets
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv');
foreach ($reader->rows() as $row) {
/** @var array{FirstName: string, LastName: string, Gender: string} $row */
// $row will be an associative array, e.g., ['FirstName' => 'Philippe', 'LastName' => 'Petit', 'Gender' => 'M']
print_r($row);
}
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv', hasHeaders: false);
foreach ($reader->rows() as $row) {
// $row will be a numeric array, e.g., [0 => 'Philippe', 1 => 'Petit', 2 => 'M']
print_r($row);
}
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv', hasHeaders: false);
$reader
->setHeader(0, 'firstname')
->setHeader(1, 'lastname')
->setHeader(2, 'gender');
foreach ($reader->rows() as $row) {
/** @var array{firstname: string, lastname: string, gender: string} $row */
// $row will be an associative array, e.g., ['firstname' => 'Philippe', 'lastname' => 'Petit', 'gender' => 'M']
print_r($row);
}
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv');
// Add a normalizer to handle missing gender data
$reader->pushNormalizer(function (array &$row): void {
/** @var array{Gender: string} $row */
if (empty($row['Gender'])) {
$row['Gender'] = 'U';
}
});
// Add another normalizer to format the gender column
$reader->pushNormalizer(function (array &$row): void{
/** @var array{Gender: string} $row */
$gender = strtolower($row['Gender']);
if (str_starts_with($gender, 'm')) {
$row['Gender'] = 'M';
} elseif (str_starts_with($gender, 'f')) {
$row['Gender'] = 'F';
}
});
// Add a normalizer to ensure Salary is an integer
$reader->pushNormalizer(function (array &$row): void {
/** @var array{Salary: string|int|null} $row */
$row['Salary'] = is_null($row['Salary']) ? 0 : (int) $row['Salary'];
});
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv');
// Add a filter to only row */
return isset($row['Salary']) && (int) $row['Salary'] > 80000;
});
// Add another filter to only
foreach ($reader->rows() as $row) {
// This line has passed all your checks and is ready to be used
print_r($row);
}
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv');
// Read rows from 10 to 20
foreach ($reader->rows(from: 10, to: 20) as $row) {
print_r($row);
}
use Inwebo\Csv\Reader;
$reader = new Reader('path/to/your/file.csv');
// Retrieve the 5th row
$row = $reader->rowAt(5);
print_r($row);
use Inwebo\Csv\Writer;
$writer = new Writer('path/to/output.csv');
$writer->setHeaders(['FirstName', 'LastName', 'Email']);
$writer->row(['Philippe', 'Petit', '[email protected]']);
$writer->row(['Marie', 'Curie', '[email protected]']);
$writer = new Writer('path/to/output.csv', bom: true);
$writer->setLineEnding("\r\n");
$writer = new Writer('path/to/output.csv');
$writer->setCsvControl(';'); // use semicolon as delimiter (common in France/Germany)
$writer->setHeader(['Prénom', 'Nom'])->row(['Philippe', 'Petit']);
use Inwebo\Csv\Writer;
$writer = new Writer('output.csv');
$headers = ['Id', 'FirstName', 'LastName', 'Email'];
$writer->setHeaders($headers);
$writer->pushFilter(function (array $row) use ($headers): bool {
return count($row) === count($headers);
});
$writer->rows([
['1', 'Alice', 'Dupont', '[email protected]'], // written
['2', 'Bob'], // skipped — only 2 columns
['3', 'Charlie', 'Martin', '[email protected]'], // written
]);
use Inwebo\Csv\Writer;
$writer = new Writer('output.csv');
// Trim whitespace and normalize casing on name columns
$writer->pushNormalizer(function (array &$row): void {
$row['FirstName'] = mb_convert_case(trim($row['FirstName']), MB_CASE_TITLE, 'UTF-8');
$row['LastName'] = mb_convert_case(trim($row['LastName']), MB_CASE_TITLE, 'UTF-8');
});
// Format phone numbers
$writer->pushNormalizer(function (array &$row): void {
$row['Phone'] = preg_replace('/\D/', '', $row['Phone'] ?? '');
});
$writer->setHeaders(['FirstName', 'LastName', 'Phone']);
$writer->rows($data);
use Inwebo\Csv\Writer;
$headers = ['Id', 'FirstName', 'LastName', 'Email', 'Salary'];
$writer = new Writer('output.csv');
$writer->setHeaders($headers);
// Skip rows with wrong column count or invalid email
$writer->pushFilter(fn(array $row) use ($headers): bool => count($row) === count($headers));
$writer->pushFilter(fn(array $row): bool => filter_var($row['Email'], FILTER_VALIDATE_EMAIL) !== false);
// Normalize names and cast salary to int
$writer->pushNormalizer(function (array &$row): void {
$row['FirstName'] = mb_convert_case(trim($row['FirstName']), MB_CASE_TITLE, 'UTF-8');
$row['LastName'] = mb_convert_case(trim($row['LastName']), MB_CASE_TITLE, 'UTF-8');
$row['Salary'] = (string) (int) $row['Salary'];
});
$writer->rows($data);
use Inwebo\Csv\Reader;
use Inwebo\Csv\Writer;
$reader = new Reader('input.csv');
$writer = new Writer('output.csv');
$writer->setHeaders($reader->getHeaders());
$writer->rows($reader->rows());