Skip to content

Commit

Permalink
Change default CSV delimiter to comma (fix #79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Jun 13, 2014
1 parent ad21938 commit 9176a04
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 29 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,18 @@ use Ddeboer\DataImport\Reader\CsvReader;

$file = new \SplFileObject('/path/to/csv_file.csv');
$reader = new CsvReader($file);
```

Optionally construct with different delimiter, enclosure and/or escape
character:

```php
$reader = new CsvReader($file, ';');
```

Then iterate over the CSV file:

```php
foreach ($reader as $row) {
// $row will be an array containing the comma-separated elements of the line:
// array(
Expand Down
2 changes: 1 addition & 1 deletion src/Ddeboer/DataImport/Reader/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CsvReader implements ReaderInterface, \SeekableIterator
* @param string $enclosure Enclosure
* @param string $escape Escape characters
*/
public function __construct(\SplFileObject $file, $delimiter = ';', $enclosure = '"', $escape = '\\')
public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
ini_set('auto_detect_line_endings', true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50;123;"Description"6;456;"Another description"7;7890;"Some more info"
50,123,"Description"6,456,"Another description"7,7890,"Some more info"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id;number;description
50;123;"Description"
6;456;"Another description"
7;7890;"Some more info"
id,number,description
50,123,"Description"
6,456,"Another description"
7,7890,"Some more info"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
id;description;description;description;details;details;last
50;"First";"Second";"Third";"Details1";"Details2";"Last one"
id,description,description,description,details,details,last
50,"First","Second","Third","Details1","Details2","Last one"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
id;number;description
50;123;"Description"
123;test
7;7890;Some more info;"too many columns"
123;66;Valid
id,number,description
50,123,"Description"
123,test
7,7890,Some more info,"too many columns"
123,66,Valid
2 changes: 1 addition & 1 deletion tests/Ddeboer/DataImport/Tests/Fixtures/data_cr_breaks.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50;123;"Description"6;456;"Another description"7;7890;"Some more info"
50,123,"Description"6,456,"Another description"7,7890,"Some more info"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id;number;description
50;123;"Description"
6;456
7;7890;"Some more info"
id,number,description
50,123,"Description"
6,456
7,7890,"Some more info"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id;number;description
50;123;"Description"
6;456;"Another description";"Some more info"
7;7890;"Yet another description"
id,number,description
50,123,"Description"
6,456,"Another description","Some more info"
7,7890,"Yet another description"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
50;123;"Description"
6;456;"Another description"
7;7890;"Some more info"
50,123,"Description"
6,456,"Another description"
7,7890,"Some more info"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
50;123;"Description"
6;"strictly invalid"
7;7890;"Some more info"
3;230;"Yet more info";"Even more info"
50,123,"Description"
6,"strictly invalid"
7,7890,"Some more info"
3,230,"Yet more info","Even more info"
"strictly invalid"

0 comments on commit 9176a04

Please sign in to comment.