Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/belguinan/csv-simple-reader
Simple PHP class for CSV manipulation. "with no dependencies"
https://github.com/belguinan/csv-simple-reader
csv csv-export csv-import csv-reader php php5
Last synced: about 2 months ago
JSON representation
Simple PHP class for CSV manipulation. "with no dependencies"
- Host: GitHub
- URL: https://github.com/belguinan/csv-simple-reader
- Owner: belguinan
- License: mit
- Created: 2020-01-22T23:20:47.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-30T02:39:01.000Z (2 months ago)
- Last Synced: 2024-10-30T03:38:14.170Z (2 months ago)
- Topics: csv, csv-export, csv-import, csv-reader, php, php5
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CSV Simple Reader
A lightweight, zero-dependency PHP library for reading, writing, and exporting CSV files. Works with PHP 5.4 and above.
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
## Features
- 🚀 Simple and intuitive API
- 📖 Memory-efficient reading of large files
- 💾 Export data to CSV files
- ⬇️ Direct CSV downloads
- 🔒 Secure file handling
- 0️⃣ Zero dependencies
- ✅ PHP 5.4+ compatible## Installation
Install via Composer:
```bash
composer require belguinan/csv-simple-reader
```## Quick Start
```php
use Belguinan\CsvExporter;// Initialize
$csv = new CsvExporter();// Read CSV file
foreach ($csv->readFrom('path/to/file.csv') as $row) {
var_dump($row);
}
```## Usage Guide
### Reading CSV Files
```php
$csv = new CsvExporter();// Read file line by line (memory efficient)
foreach ($csv->readFrom('input.csv') as $row) {
// $row is an array containing the CSV columns
var_dump($row);
}
```### Creating CSV Files
```php
// Your data as array
$data = array(
array('John', 'Doe', '[email protected]'),
array('Jane', 'Smith', '[email protected]')
);// Optional headers
$headers = array('First Name', 'Last Name', 'Email');// Create CSV exporter
$csv = new CsvExporter($data, $headers);// Process and save
$csv->process()->save('output.csv');
```### Downloading CSV Files
```php
// Create and force download
$csv = new CsvExporter($data, $headers);
$csv->process()->download('users-export');
```### Chaining Operations
```php
// Process, download, and save in one go
$csv->process()
->download('export-file')
->save('backup/export.csv');
```## Error Handling
The library throws `Exception` for various error conditions. It's recommended to wrap operations in try-catch blocks:
```php
try {
$csv = new CsvExporter($data);
$csv->process()->save('output.csv');
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}
```## Common Exceptions
- File not found
- File not readable
- Directory not writable
- Invalid CSV data structure
- Memory stream errors## Best Practices
1. **Reading Large Files**
```php
// Good - Memory efficient
foreach ($csv->readFrom('large.csv') as $row) {
processRow($row);
}
```2. **Setting Headers**
```php
// Explicit headers
$headers = array('ID', 'Name', 'Email');
$csv = new CsvExporter($data, $headers);// Auto-generated headers from data keys
$csv = new CsvExporter($data);
```3. **Error Handling**
```php
try {
$csv->readFrom('file.csv');
} catch (\Exception $e) {
log_error($e->getMessage());
// Handle error appropriately
}
```## Examples
### Export Users Table
```php
// Fetch users from database
$users = $db->query('SELECT id, name, email FROM users');// Convert to array
$data = array();
while ($row = $users->fetch_assoc()) {
$data[] = $row;
}// Export
$csv = new CsvExporter($data);
$csv->process()->download('users-export');
```### Process CSV in Chunks
```php
$csv = new CsvExporter();
$chunk = array();foreach ($csv->readFrom('large-file.csv') as $index => $row) {
$chunk[] = $row;
// Process in chunks of 1000
if (count($chunk) >= 1000) {
processChunk($chunk);
$chunk = array();
}
}// Process remaining rows
if (!empty($chunk)) {
processChunk($chunk);
}
```## License
MIT License - feel free to use this library in your projects.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## Support
For bugs and feature requests, please use the [GitHub issue tracker](https://github.com/belguinan/csv-simple-reader/issues).