|
1
|
|
|
<?php |
|
2
|
|
|
namespace CsvCombine\Import; |
|
3
|
|
|
|
|
4
|
|
|
use CsvCombine\Import\AppImport; |
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class CsvImport extends AppImport |
|
8
|
|
|
{ |
|
9
|
|
|
/* |
|
10
|
|
|
* loadDataCsv CSV読み込みアクション |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $fileName 固定長テキストファイ |
|
13
|
|
|
* @param array $columnList 各カラム情報(name:カラム名,length:バイト数) |
|
14
|
|
|
* @param array $options |
|
15
|
|
|
*/ |
|
16
|
|
|
public function import($fileName, $columnList = [], $options = []) |
|
17
|
|
|
{ |
|
18
|
|
|
$options = $this->getOptions($options); |
|
19
|
|
|
extract($options); |
|
20
|
|
|
|
|
21
|
|
|
//保存をするのでモデルを読み込み |
|
22
|
|
|
try { |
|
23
|
|
|
$data = array(); |
|
24
|
|
|
$csvData = array(); |
|
25
|
|
|
$file = fopen($fileName,"r"); |
|
26
|
|
|
while($data = $this->fgetcsv_reg($file, 65536, $delimiter)) {//CSVファイルを","区切りで配列に |
|
27
|
|
|
mb_convert_variables($arrayEncoding, $importEncoding, $data); |
|
28
|
|
|
$csvData[] = $data; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$i = 0; |
|
32
|
|
|
foreach ($csvData as $line) { |
|
33
|
|
|
$thisData = array(); |
|
34
|
|
|
if (empty($columnList)) { |
|
35
|
|
|
$thisColumnList = array(); |
|
36
|
|
|
$lineCount = 0; |
|
37
|
|
|
foreach ($line as $line_v) { |
|
38
|
|
|
$thisColumnList[] = $lineCount; |
|
39
|
|
|
$lineCount++; |
|
40
|
|
|
} |
|
41
|
|
|
} else { |
|
42
|
|
|
$thisColumnList = $columnList; |
|
43
|
|
|
} |
|
44
|
|
|
foreach ($thisColumnList as $k => $v) { |
|
45
|
|
|
if (isset($line[$k])) { |
|
46
|
|
|
//先頭と末尾の"を削除 |
|
47
|
|
|
$b = $line[$k]; |
|
48
|
|
|
//カラムの数だけセット |
|
49
|
|
|
$thisData = array_merge( |
|
50
|
|
|
$thisData, |
|
51
|
|
|
array($v => $b) |
|
52
|
|
|
); |
|
53
|
|
|
} else { |
|
54
|
|
|
$thisData = array_merge( |
|
55
|
|
|
$thisData, |
|
56
|
|
|
array($v => '') |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$data[$i] = $thisData; |
|
62
|
|
|
$i++; |
|
63
|
|
|
} |
|
64
|
|
|
} catch (Exception $e) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $data; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* fgetcsv_reg |
|
73
|
|
|
* |
|
74
|
|
|
* this is a port of the original code written by yossy. |
|
75
|
|
|
* |
|
76
|
|
|
* @author yossy |
|
77
|
|
|
* @author hagiwara |
|
78
|
|
|
* |
|
79
|
|
|
* @param resource $handle |
|
80
|
|
|
* @param integer $length |
|
81
|
|
|
* @param string $d |
|
82
|
|
|
* @param string $e |
|
83
|
|
|
* @see http://yossy.iimp.jp/wp/?p=56 |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
private function fgetcsv_reg (&$handle, $length = null, $d = ',', $e = '"') |
|
87
|
|
|
{ |
|
88
|
|
|
$d = preg_quote($d); |
|
89
|
|
|
$e = preg_quote($e); |
|
90
|
|
|
$_line = ""; |
|
91
|
|
|
$eof = false; // Added for PHP Warning. |
|
92
|
|
|
while ( $eof != true ) { |
|
|
|
|
|
|
93
|
|
|
$_line .= (empty($length) ? fgets($handle) : fgets($handle, $length)); |
|
94
|
|
|
$itemcnt = preg_match_all('/'.$e.'/', $_line, $dummy); |
|
95
|
|
|
if ($itemcnt % 2 == 0) $eof = true; |
|
96
|
|
|
} |
|
97
|
|
|
$_csv_line = preg_replace('/(?:\\r\\n|[\\r\\n])?$/', $d, trim($_line)); |
|
98
|
|
|
$_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/'; |
|
99
|
|
|
|
|
100
|
|
|
preg_match_all($_csv_pattern, $_csv_line, $_csv_matches); |
|
101
|
|
|
|
|
102
|
|
|
$_csv_data = $_csv_matches[1]; |
|
103
|
|
|
|
|
104
|
|
|
for ( $_csv_i=0; $_csv_i<count($_csv_data); $_csv_i++ ) { |
|
|
|
|
|
|
105
|
|
|
$_csv_data[$_csv_i] = preg_replace('/^'.$e.'(.*)'.$e.'$/s', '$1', $_csv_data[$_csv_i]); |
|
106
|
|
|
$_csv_data[$_csv_i] = str_replace($e.$e, $e, $_csv_data[$_csv_i]); |
|
107
|
|
|
} |
|
108
|
|
|
return empty($_line) ? false : $_csv_data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.