{"id":15308192,"url":"https://github.com/yidas/csv-php","last_synced_at":"2025-02-26T13:31:14.220Z","repository":{"id":57086783,"uuid":"402467314","full_name":"yidas/csv-php","owner":"yidas","description":"PHP CSV writer \u0026 reader with settings of quoting enclosure and encoding","archived":false,"fork":false,"pushed_at":"2021-09-06T11:02:11.000Z","size":4,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-07T00:51:19.410Z","etag":null,"topics":["csv","csv-reader","csv-writer","double-quotes","encoding","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-02T15:15:48.000Z","updated_at":"2024-01-20T07:04:33.000Z","dependencies_parsed_at":"2022-08-25T00:50:46.004Z","dependency_job_id":null,"html_url":"https://github.com/yidas/csv-php","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcsv-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcsv-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcsv-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcsv-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidas","download_url":"https://codeload.github.com/yidas/csv-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239937702,"owners_count":19721483,"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-reader","csv-writer","double-quotes","encoding","php"],"created_at":"2024-10-01T08:14:28.329Z","updated_at":"2025-02-26T13:31:13.768Z","avatar_url":"https://github.com/yidas.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://codeigniter.com/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://www.php.net/images/logos/php-logo-bigger.png\" height=\"60px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003ePHP CSV\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nPHP CSV writer \u0026amp; reader with settings of quoting enclosure and encoding\n\n[![Latest Stable Version](https://poser.pugx.org/yidas/csv/v/stable?format=flat-square)](https://packagist.org/packages/yidas/csv)\n[![License](https://poser.pugx.org/yidas/csv/license?format=flat-square)](https://packagist.org/packages/yidas/csv)\n\n\nFeatures\n--------\n\n- *Support generating **Double Quotes** enclosure for all entities setting*\n\n- *Support **Encoding** setting for local dialect*\n\n- ***Elegent Interface** for setup and use*\n\n---\n\nOUTLINE\n-------\n\n- [Demonstration](#demonstration)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Usage](#usage)\n    - [Options](#options)\n    - [Writer](#writer)\n    - [Reader](#reader)\n    - [Exceptions](#exceptions) \n- [References](#references)\n\n---\n\nDEMONSTRATION\n-------------\n\nQuickstart with specifying the file name directly:\n\n```php\n// Read the CSV file\n$csvReader = new yidas\\csv\\Reader('/tmp/file.csv');\n$rows = $csvReader-\u003ereadRows();\n$csvReader-\u003efclose();\n// Write the CSV file\n$csvWriter = new yidas\\csv\\Writer('/tmp/file.csv'); \n$csvWriter-\u003ewriteRows($rows);\n$csvWriter-\u003efclose();\n```\n\n### Write to CSV\n\nOpen a file and use libray to write in CSV format:\n\n```php\n$fp = fopen(\"/tmp/file.csv\", 'w');\n\n$csvWriter = new yidas\\csv\\Writer($fp, [\n    // 'quoteAll' =\u003e true,\n    // 'encoding' =\u003e 'UTF-8'\n]); \n$csvWriter-\u003ewriteRow([\"First\", 'Second']);\n$csvWriter-\u003ewriteRows([\n    [\"Normal\", 'Double\"Quote'], \n    [\"It's a context,\\nNew line.\", 'Encoded中文'],\n]);\n\nfclose($fp);\n```\n\nThe content of generated CSV file will be:\n\n```csv\n\"First\",\"Second\"\n\"Normal\",\"Double\"\"Quote\"\n\"It's a context,\nNew line.\",\"Encoded中文\"\n```\n\n\u003e In default setting, it will always add double quotes with `UTF-8` encoding.\n\n### Read from CSV\n\nOpen a CSV file with local encoding (`Big5`) and use libray to read with `UTF-8` encoding:\n\n```php\n$fp = fopen(\"/tmp/file.csv\", 'r');\n\n$csvReader = new yidas\\csv\\Reader($fp, [\n    'encoding' =\u003e 'Big5'\n]); \n$firstRow = $csvReader-\u003ereadRow();\n$remainingRows = $csvReader-\u003ereadRows();\n\nfclose($fp);\n```\n\n---\n\nREQUIREMENTS\n------------\n\nThis library requires the following:\n\n- PHP CLI 5.4.0+\n\n---\n\nINSTALLATION\n------------\n\nRun Composer in your project:\n\n    composer require yidas/csv\n    \nThen you could use the class after Composer is loaded on your PHP project:\n\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse yidas\\csv\\Writer;\nuse yidas\\csv\\Reader;\n```\n\n---\n\nUSAGE\n-----\n\n### Options\n\nThe options in parameter 2 for Writer/Reader class are as below:\n\n#### quoteAll\n\nControls when quotes should be always generated by the writer.\n\n#### Encoding\n\nControls which the encoding should be generated by the writer/reader.\n\n\u003e By defaule, Microsoft Excel will open CSV file with local encoding. \n\u003e For example: Excel in Chinese(Traditional) Windows will open CSV with Big5 encoding.\n\n### Writer\n\n```php\n$csvWriter = new yidas\\csv\\Writer($fp, [\n    // 'quoteAll' =\u003e true,\n    // 'encoding' =\u003e 'UTF-8'\n]); \n```\n\n#### writeRow()\n\nWrite the row parameter to the writer’s file stream, formatted according to the current setting.\n\n```php\npublic static array writeRow(array $rowData)\n```\n\n#### writeRows()\n\nWrite the rows parameter to the writer’s file stream, formatted according to the current setting.\n\n```php\npublic static array writeRows(array $rowsData)\n```\n\n### Reader\n\n```php\n$csvReader = new yidas\\csv\\Reader($fp, [\n    // 'encoding' =\u003e 'UTF-8'\n]); \n```\n\n#### readRow()\n\nRead a row from current file pointer.\n\n```php\npublic static array readRow()\n```\n\n*Example:*\n\n```php\nwhile ( ($row = $csvReader-\u003ereadRow($file) ) !== FALSE ) {\n    var_dump($row);\n}\n```\n\n#### readRows()\n\nRead all the rows from current file pointer.\n\n```php\npublic static array readRows()\n```\n\n*Example:*\n\n```php\n$rows = $csvReader-\u003ereadRows();\nvar_dump($rows);\n```\n\n### Exceptions\n\n```php\n\ntry {\n\n    $csvWriter = new yidas\\csv\\Writer($fp, [\n        // 'quoteAll' =\u003e true,\n        // 'encoding' =\u003e 'UTF-8'\n    ]); \n    $csvWriter-\u003ewriteRow([\"First\", 'Second']);\n    \n} catch (\\Exception $e) {\n\n    echo 'Code:' . $e-\u003egetCode() . ' Message:' . $e-\u003egetMessage();\n}\n```\n\n---\n\nREFERENCES\n----------\n\n- [Comma-separated values - Basic rules - Wiki](https://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules)\n\n- [RFC 4180 - Common Format and MIME Type for Comma-Separated Values (CSV) Files](https://datatracker.ietf.org/doc/html/rfc4180)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fcsv-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidas%2Fcsv-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fcsv-php/lists"}