1: <?php
2: /**
3: * helper functions for csv processing
4: */
5:
6: /**
7: * read csv into named array
8: * @param type $filename
9: * @return type
10: */
11: function readfromfile($filename) {
12: $line1 = getFirstLine($filename);
13: $dataset = array();
14: $row = 0;
15: if (($handle = fopen($filename, "r")) !== FALSE) {
16: while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {
17: $num = count($data);
18: if ($row > 0) {
19: for ($i = 0; $i < $num; $i++) {
20: $dataset[$row][trim($line1[$i])] = $data[$i];
21: }
22: }
23: $row++;
24: }
25: fclose($handle);
26: return $dataset;
27: }
28: }
29: /**
30: * Get first line which is considered to contain Headline with common names
31: * @param type $filename
32: * @return type
33: * @throws Exception
34: */
35: function getFirstLine($filename) {
36: $file = fopen($filename, "r");
37:
38: if (!$file) {
39: throw new Exception("can not find file" . $filename);
40: }
41:
42: $line1 = fgetcsv($file, 0, '|');
43: fclose($file);
44: return $line1;
45: }
46:
47: /**
48: * Spilt $value by separators $sep
49: * @param type $values
50: * @param type $sep
51: * @return array
52: */
53: function valueSplit($values, $sep) {
54: if ($values == '') {
55: $earray = array();
56: return $earray;
57: }
58: $spilted = preg_split("/[" . $sep . "]+/", $values);
59: if (is_array($spilted)) {
60: foreach ($spilted as $element) {
61: trim($element);
62: }
63:
64: return $spilted;
65: } else {
66: $spiltinarray = array(trim($spilted));
67: return $spiltinarray;
68: }
69: }
70: /**
71: * checks if $values contains separators from array $sepas
72: * @param String $values
73: * @param array $sepas
74: * @return bool|array
75: */
76: function checkForSeparators(String $values, array $sepas) {
77: $countsep = 0;
78: $fsep = '';
79: foreach ($sepas as $sepa) {
80: echo gettype($sepa);
81: echo $values;
82: if (stripos($values, $sepa)) {
83: $countsep = $countsep + 1;
84: $fsepa = $sepa;
85: }
86: }
87: if ($countsep == 1) {
88: return $fsepa;
89: } elseif ($countsep > 1) {
90: return true;
91: } else {
92: return false;
93: }
94: }
95: