File: index.md

package info (click to toggle)
php-league-csv 9.23.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 39,720 kB
  • sloc: php: 13,345; javascript: 80; makefile: 29; xml: 29
file content (143 lines) | stat: -rw-r--r-- 3,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
layout: homepage
---

# Features

## Accessing CSV documents records

`Reader`, the read only connection object enables accessing CSV records easily

```php
use League\Csv\Reader;

//load the CSV document from a file path
$csv = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
$csv->setHeaderOffset(0);

$header = $csv->getHeader(); //returns the CSV header record

//returns all the records as
$records = $csv->getRecords(); // an Iterator object containing arrays
$records = $csv->getRecordsAsObject(MyDTO::class); //an Iterator object containing MyDTO objects

echo $csv->toString(); //returns the CSV document as a string
```

## Adding new CSV records is made simple

`Writer`, the write only connection object enables adding one or more records in one call.

```php
use League\Csv\Writer;

$header = ['first name', 'last name', 'email'];
$records = [
    [1, 2, 3],
    ['foo', 'bar', 'baz'],
    ['john', 'doe', 'john.doe@example.com'],
];

//load the CSV document from a string
$csv = Writer::createFromString();

//insert the header
$csv->insertOne($header);

//insert all the records
$csv->insertAll($records);

echo $csv->toString(); //returns the CSV document as a string
```

## Advanced CSV records selection

`Statement`, the constraint builder object ease CSV records selection

```php
use League\Csv\Reader;
use League\Csv\Statement;

//load the CSV document from a stream
$stream = fopen('/path/to/your/csv/file.csv', 'r');
$csv = Reader::createFromStream($stream);
$csv->setDelimiter(';');
$csv->setHeaderOffset(0);

//build a statement
$stmt = new Statement()
    ->select('firstname', 'lastname', 'email')
    ->andWhere('firstname', 'starts with', 'A')
    ->orderByAsc('email')
    ->offset(10)
    ->limit(25);

//query your records from the document
$records = $stmt->process($csv);
foreach ($records as $record) {
    //do something here
}
```

## CSV documents converters

Different converters objects ease transforming your CSV documents into other popular formats

```php
use League\Csv\Reader;
use League\Csv\XMLConverter;

//load the CSV document from a SplFileObject
$file = new SplFileObject('/path/to/your/csv/file.csv', 'r');
$csv = Reader::createFromFileObject($file);

$converter = new XMLConverter()
    ->rootElement('csv')
    ->recordElement('record', 'offset')
    ->fieldElement(null);

$dom = $converter->convert($csv);
$dom->formatOutput = true;
$dom->encoding = 'iso-8859-15';

echo '<pre>', PHP_EOL;
echo htmlentities($dom->saveXML());
// <?xml version="1.0" encoding="iso-8859-15"?>
// <csv>
//   ...
//   <record offset="71">
//     <prenoms>Anaïs</prenoms>
//     <nombre>137</nombre>
//     <sexe>F</sexe>
//     <annee>2004</annee>
//   </record>
//   ...
//   <record offset="1099">
//     <prenoms>Anaïs</prenoms>
//     <nombre>124</nombre>
//     <sexe>F</sexe>
//     <annee>2005</annee>
//   </record>
// </csv>
```

## Supports PHP Stream filter API

PHP stream filters can directly be used to ease manipulating CSV document

```php
use League\Csv\Reader;
use League\Csv\Bom;

$csv = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
$csv->setHeaderOffset(0);

if (Bom::tryFromSequence($csv)?->isUtf16() ?? false) {
    $csv->appendStreamFilterOnRead('convert.iconv.UTF-16/UTF-8');
}

foreach ($csv as $record) {
    //all fields from the record are converted from UTF-16 into UTF-8 charset
    //and the BOM sequence is removed
}
```