{"id":16467109,"url":"https://github.com/wilgucki/php-csv","last_synced_at":"2025-03-23T11:32:37.771Z","repository":{"id":57080771,"uuid":"132229287","full_name":"wilgucki/php-csv","owner":"wilgucki","description":null,"archived":false,"fork":false,"pushed_at":"2020-07-04T08:32:05.000Z","size":36,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T11:46:04.774Z","etag":null,"topics":["csv","csv-parser","csv-reader","csv-writer","php"],"latest_commit_sha":null,"homepage":"https://blog.wilgucki.pl","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wilgucki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-05T08:31:02.000Z","updated_at":"2023-11-07T02:29:53.000Z","dependencies_parsed_at":"2022-08-24T13:10:23.134Z","dependency_job_id":null,"html_url":"https://github.com/wilgucki/php-csv","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilgucki%2Fphp-csv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilgucki%2Fphp-csv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilgucki%2Fphp-csv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilgucki%2Fphp-csv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wilgucki","download_url":"https://codeload.github.com/wilgucki/php-csv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221849824,"owners_count":16891536,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","csv-parser","csv-reader","csv-writer","php"],"created_at":"2024-10-11T11:46:05.815Z","updated_at":"2024-10-28T15:40:51.620Z","avatar_url":"https://github.com/wilgucki.png","language":"PHP","readme":"# PHP-CSV\n\nPHP-CSV is a package that allows you to manage CSV files in an object-oriented way.\n\n## Installation\n\nThere's not much to do, just run this composer command\n\n```\ncomposer require wilgucki/php-csv\n```\n\n## Usage\n\nBoth, Reader and Writer classes are easy to use. In fact the classes are just a wrappers for built-in PHP functions.\n\n### Reader\n\nReader class will help you with reading existing CSV files (no surprise here).\n\nTo read CSV file you need to open it first.\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\n```\n\nConstructor accepts optional arguments you can use to describe your CSV file. The arguments are:\n\n* `$delimiter`\n* `$enclosure`\n* `$escape`\n* `$encodingFrom`\n* `$encodingTo`\n\nFirst three arguments are exactly the same as fgetcsv function arguments described in the manual - http://php.net/manual/en/function.fgetcsv.php\n\nOther two helps you define encoding - `$encodingFrom` is the encoding of CSV file and `$encodingTo` is encoding we get after file is read.\n\nTo read data from the CSV file we can use two functions: `readLine` and `readAll`. The former will read and return current row while the latter will\nread whole CSV file and return it as an array of arrays (each line will be represented by an array).\n\nIf the CSV file has a header row, you can use `getHeader` function. This function will take the first row from CSV file and use it to set array keys\nfor `readLine` and `readAll` functions. This means that instead of numeric keys you can use labels defined in the first row.\n\n#### Converters\n\nSometimes data available in csv file need to be converterd into more suitable format, e.g. convert dates into Carbon objects.\nConverters make this task much easier. All you need to do is to create converter object and specify the column you want to\nconvert.\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\n$reader-\u003eaddConverter(3, new DateToCarbon());\n$data = $reader-\u003ereadLine();\n```\n\nYou can assigon only one converter per column.\n\n**Examples**\n\nCSV file example\n```\nuser_id,name\n1,john\n2,jane\n```\n\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\nprint_r($reader-\u003ereadLine());\n/*\nArray\n(\n    [0] =\u003e user_id\n    [1] =\u003e name\n)\n*/\nprint_r($reader-\u003ereadLine());\n/*\nArray\n(\n    [0] =\u003e 1\n    [1] =\u003e john\n)\n*/\n\n```\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\nprint_r($reader-\u003ereadAll());\n/*\nArray\n(\n    [0] =\u003e Array\n        (\n            [0] =\u003e user_id\n            [1] =\u003e name\n        )\n\n    [1] =\u003e Array\n        (\n            [0] =\u003e 1\n            [1] =\u003e john\n        )\n\n    [2] =\u003e Array\n        (\n            [0] =\u003e 2\n            [1] =\u003e jane\n        )\n\n)\n*/\n```\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\nprint_r($reader-\u003egetHeader());\n/*\nArray\n(\n    [0] =\u003e user_id\n    [1] =\u003e name\n)\n*/\n\n```\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\n$reader-\u003egetHeader();\nprint_r($reader-\u003ereadLine());\n/*\nArray\n(\n    [user_id] =\u003e 1\n    [name] =\u003e john\n)\n*/\n\n```\n\n```php\n$reader = new Reader();\n$reader-\u003eopen('/path/to/file.csv');\n$reader-\u003egetHeader();\nprint_r($reader-\u003ereadAll());\n/*\nArray\n(\n    [0] =\u003e Array\n        (\n            [user_id] =\u003e 1\n            [name] =\u003e john\n        )\n\n    [1] =\u003e Array\n        (\n            [user_id] =\u003e 2\n            [name] =\u003e jane\n        )\n\n)\n*/\n\n```\n\nDon't forget to close CSV file after you are done.\n\n```php\n$reader-\u003eclose();\n```\n\n### Writer\n\nFor creating/updating CSV files you can use Writer class. If you want to create file, you need to provide only a writable path.\nFor updating existing file, you have to use optional `$mode` argument. All available modes are described in the manual\n(https://secure.php.net/manual/en/function.fopen.php) but only 'w' and 'a' values are usable in this case.\n\n```php\n$writer = new Writer();\n\n// create new file\n$writer-\u003ecreate('/path/to/file.csv');\n\n// update existing file\n$writer-\u003ecreate('/path/to/file.csv', 'a+');\n```\n\n`Writer` contructor accepts the same arguments as the `Reader` constructor. Only difference is that `$encodingFrom` refers to the input encoding and the \n`$encodingTo` refers to CSV file encoding.\n\nThere are two ways of writing CSV files - `writeLine` and `writeAll`. First method will write a single line to a CSV file, while the second method will\nwrite multiple lines. If for some reason you need to access data you have written to a CSV file you can use `flush` method.\n\n**Examples**\n\n```php\n// write a single line to the CSV file\n\n$writer = new Writer();\n$writer-\u003ecreate('/path/to/file.csv');\n$writer-\u003ewriteLine(['abc', 'def']);\n```\n\n```php\n// write multiple lines to the CSV file\n\n$writer = new Writer();\n$writer-\u003ecreate('/path/to/file.csv');\n$writer-\u003ewriteAll([\n    ['abc', 'def'],\n    [123, 234]\n]);\n```\n\n```php\n// display added data\n\n$writer = new Writer();\n$writer-\u003ecreate('/path/to/file.csv');\n$writer-\u003ewriteAll([\n    ['abc', 'def'],\n    [123, 234]\n]);\necho $writer-\u003eflush();\n/*\nabc,def\n123,234\n*/\n```\n\nDon't forget to close CSV file after you are done.\n\n```php\n$writer-\u003eclose();\n```\n\n## TODO\n\n- processors - process csv data and return the results\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilgucki%2Fphp-csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilgucki%2Fphp-csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilgucki%2Fphp-csv/lists"}