forked from synappnz/php-csv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv.php
156 lines (147 loc) · 3.16 KB
/
csv.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
class csv
{
private $m_csv;
private $m_count;
private $m_index;
private $m_rows;
private function row()
{
$row = [];
$column = '';
while ($this->m_index < $this->m_count)
{
$character = $this->m_csv[$this->m_index++];
if ($character == '"')
{
while ($this->m_index < $this->m_count)
{
$character = $this->m_csv[$this->m_index++];
if ($character == '"')
{
/*
* If a double quote is found in a double quoted column then the following character must be:
*
* - A double quote which is the second character of an escaped double quote.
* - A comma which is the end of the column.
* - A CR, CR LF or LF which is the end of the column and the end of the row.
* - An EOF which is the end of the column, the end of the row and the end of the file.
*
* Anything else is an error.
*/
if ($this->m_index < $this->m_count)
{
$character = $this->m_csv[$this->m_index++];
if ($character == '"')
{
$column .= $character;
}
else if ($character == ',')
{
$row[] = $column;
$column = '';
break;
}
else if ($character == "\r")
{
if ($this->m_index < $this->m_count)
{
$character = $this->m_csv[$this->m_index];
if ($character == "\n")
{
$this->m_index++;
}
}
$row[] = $column;
return $row;
}
else if ($character == "\n")
{
$row[] = $column;
return $row;
}
else
{
throw new Exception('Unescaped double quote found in double quote enclosed column.');
}
}
else
{
$row[] = $column;
return $row;
}
}
else
{
$column .= $character;
}
}
if ($this->m_index == $this->m_count)
{
throw new Exception('Unterminated double quoted column found.');
}
}
else
{
while (true)
{
if ($character == ',')
{
$row[] = $column;
$column = '';
break;
}
else if ($character == "\r")
{
if ($this->m_index < $this->m_count)
{
$character = $this->m_csv[$this->m_index];
if ($character == "\n")
{
$this->m_index++;
}
}
$row[] = $column;
return $row;
}
else if ($character == "\n")
{
$row[] = $column;
return $row;
}
else
{
$column .= $character;
}
if ($this->m_index < $this->m_count)
{
$character = $this->m_csv[$this->m_index++];
}
else
{
$row[] = $column;
return $row;
}
}
}
}
}
public function __construct($p_csv)
{
$this->m_csv = $p_csv;
$this->m_count = strlen($this->m_csv);
$this->m_index = 0;
}
public function rows()
{
if (!isset($this->m_rows))
{
$this->m_rows = [];
while ($this->m_index < $this->m_count)
{
$this->m_rows[] = $this->row();
}
}
return $this->m_rows;
}
}