-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.php
executable file
·62 lines (38 loc) · 1 KB
/
parser.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
<?php
class Parser {
public $path;
public function __construct($path) {
$this->path = $path;
}
// convert comma delimeted txt to array
public function read() {
// read file
$data = file(__DIR__ . '/' . $this->path, FILE_IGNORE_NEW_LINES);
return $data;
}
// converts array to comma separated and writes to txt
public function write($data) {
// remove newlines, etc from strings
$clensed = $this->sanitize($data);
// create file (append)
$fh = fopen($this->path, 'a+');
foreach ($clensed as $arr) {
fputcsv($fh, array_values($arr), ',', '"');
}
fclose($fh);
return true;
}
// sanitize
public function sanitize($arr) {
$clensed = array();
foreach ($arr as $key => $value) {
// remove commas from customer data
$value = str_replace(',', '', $value);
// remove newline charactars
$value = str_replace(PHP_EOL, ' ', $value);
// update with sanitized value
$clensed[$key] = $value;
}
return $clensed;
}
}