1. Go to this page and download the library: Download offdev/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/ */
offdev / csv example snippets
use Offdev\Csv\Stream;
$stream = Stream::factory(fopen('/path/to/file.csv', 'r'));
use Offdev\Csv\Stream;
$stream = Stream::factory('this string will be transformed to an in-memory stream');
use GuzzleHttp\Client;
use Offdev\Csv\Stream;
$client = new Client();
$response = $client->get('http://httpbin.org/get');
$stream = Stream::factory($response->getBody());
// Recognizes files, and opens them in read mode
$fileStream = stream('/tmp/results.csv');
// Create from string
$stringStream = stream('stream content');
// From objcets, which implement the __toString method
class Example
{
public function __toString(){
return 'some example';
}
}
$objectToStringStream = stream(new Example());
use Offdev\Csv\Parser;
$parser = new Parser($stream);
while (!$parser->eof()) {
$record = $parser->readLine();
echo $record->get('header-column2').PHP_EOL;
}
$parser = new Parser($stream);
foreach ($parser as $index => $record) {
echo $record->get('header-column2').PHP_EOL;
}
$parser = new Parser($stream, [
Parser::OPTION_DELIMITER => ';'
]);
namespace MyCompany\ProjectX\Processors;
use Offdev\Csv\Item;
use Offdev\Csv\ProcessorInterface;
class MyProcessor implements ProcessorInterface
{
public function processRecord(Item $record): void
{
// No header in CSV, use numeric index
echo "Got item: ".$record->get(1).PHP_EOL;
}
public function processInvalidRecord(Item $record): void
{
$this->processRecord($record);
}
public function eof(): void
{
echo "---EOF---".PHP_EOL;
}
}
use MyCompany\ProjectX\Processors\MyProcessor;
use Offdev\Csv\Parser;
use Offdev\Csv\Stream;
$stream = Stream::factory("1;John\n2;Lisa\n3;Robert");
$parser = new Parser($stream, [
Parser::OPTION_DELIMITER => ';',
Parser::OPTION_HEADER => false
]);
$parser->setProcessor(new MyProcessor());
$parser->run();
use Offdev\Csv\Parser;
use Offdev\Csv\Stream;
use Offdev\Csv\Validator;
try {
$stream = Stream::factory("id,name\n1,John\n2,Lisa\nNaN,Robert");
$parser = new Parser($stream);
$parser->setValidator(new Validator([
'id' => '
$ php example.php
row1-value2
row2-value2
$ php example.php
Got item: John
Got item: Lisa
Got item: Robert
---EOF---
$ ./vendor/bin/phpcs --colors --standard=PSR2 -v src/ tests/
Registering sniffs in the PSR2 standard... DONE (42 sniffs registered)
Creating file list... DONE (10 files in queue)
Changing into directory /Users/pascal/devel/csv-parser/src
Processing Validator.php [PHP => 436 tokens in 74 lines]... DONE in 46ms (0 errors, 0 warnings)
Processing Parser.php [PHP => 2125 tokens in 312 lines]... DONE in 140ms (0 errors, 0 warnings)
Processing Stream.php [PHP => 2248 tokens in 344 lines]... DONE in 116ms (0 errors, 0 warnings)
Processing ParserInterface.php [PHP => 552 tokens in 115 lines]... DONE in 27ms (0 errors, 0 warnings)
Processing ProcessorInterface.php [PHP => 168 tokens in 36 lines]... DONE in 21ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/csv-parser/tests
Processing ParserTest.php [PHP => 1797 tokens in 214 lines]... DONE in 149ms (0 errors, 0 warnings)
Processing TestProcessor.php [PHP => 427 tokens in 80 lines]... DONE in 31ms (0 errors, 0 warnings)
Processing ValidatorTest.php [PHP => 179 tokens in 33 lines]... DONE in 14ms (0 errors, 0 warnings)
Processing StreamTest.php [PHP => 1647 tokens in 217 lines]... DONE in 124ms (0 errors, 0 warnings)
Processing InvalidStream.php [PHP => 999 tokens in 210 lines]... DONE in 57ms (0 errors, 0 warnings)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.