forked from crosbymichael/php-csv-to-xml-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVParserBase.php
43 lines (34 loc) · 901 Bytes
/
CSVParserBase.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
<?php
/**
* Abstract base class that provides a common interface for the parser
* along with shared methods and properties.
*/
abstract class CSVParserBase {
protected $MaxLenght = 0;
protected $Separator = '';
public $HeaderArray = null;
public $IsFirstRowHeader = false;
function __construct($maxLenght = 1000, $separator = ',') {
$this->MaxLenght = $maxLenght;
$this->Separator = $separator;
}
public function Parse($csvData) {
$array = null;
if (($fh = fopen($csvData, "r"))) {
while (($data = fgetcsv($fh, $this->MaxLenght, $this->Separator))) {
$array[] = $data;
}
fclose($fh);
} else {
throw new Exception("Cannot parse data", 1);
}
return $this->ProcessArray($array);
}
protected abstract function ProcessArray($array);
protected function StripString($contents) {
return preg_replace(
'/\s+/',
'',
$contents);;
}
}