-
Notifications
You must be signed in to change notification settings - Fork 1
/
CsvParser.php
executable file
·67 lines (59 loc) · 1.55 KB
/
CsvParser.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
<?php
namespace PYSys\Tools;
use Nette\Object;
use Nette\Utils\ArrayHash;
/**
* Class CsvParser
* @package PYSys\Tools
*
* For parsing CSV files
*/
class CsvParser extends Object
{
public static function from($file,$delimiter=";",$limit=null,$offset=null) {
$file = fopen($file, "r");
if (!$file) {
throw new CsvParserException("Cannot read/open file");
}
$data = array();
$columns = array();
while($line = fgetcsv($file,4096, $delimiter)) {
$columns = $line;
break;
}
foreach ($columns as $key => $column) {
$columns[$key] = iconv("windows-1250",'utf-8',$column);
}
$i=0;
$l=0;
while($line = fgetcsv($file,4096, $delimiter)) {
$i++; if($offset !== null && $offset > $i) continue;
$l++;
$paired_colums = array();
foreach ($columns as $key => $column) {
if(isset($line[$key]))
$paired_colums[$column] = iconv("windows-1250",'utf-8',$line[$key]);
//$paired_colums[$column] = $line[$key];
}
$data[] = $paired_colums;
if($limit !== null && $limit <= $l) break;
}
fclose($file);
$return = new ArrayHash();
$return->columns = ArrayHash::from($columns);
$return->data = ArrayHash::from($data);
return $return;
}
public static function to($filename,$csv_data) {
if(file_exists($filename)) {
unlink($filename);
}
file_put_contents($filename,"");
foreach ($csv_data as $line) {
$csv_line = iconv('utf-8','windows-1250',"\"".implode("\";\"",$line)."\"\r\n");
file_put_contents($filename,$csv_line,FILE_APPEND);
}
return true;
}
}
class CsvParserException extends \Exception {}