This repository has been archived by the owner on Mar 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
180 lines (137 loc) · 4.06 KB
/
functions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*var_dump(parseNullValue("dwed?ded"));
var_dump(parseNullValue("hey None"));
var_dump(parseNullValue(0));
var_dump(parseNullValue("?"));
var_dump(parseNullValue("NULL"));
var_dump(parseNullValue("[none]"));
var_dump(parseNullValue("[nn]"));
var_dump(parseNullValue("(unknown)"));*/
function parseNullValue($s) {
if(empty($s) && $s!='0') return true;
//if(is_numeric($s)) return false; // handle 0 values of id
$nullValues = ['NULL', '[nn]', 'nn', 'none', '[none]','?', '(unknown)','None'];
foreach($nullValues as $n) {
if ($s === $n) {
return true;
}
}
return false;
}
/*echo parseDoubleQuote('"fweihfekf"') . "<br/>";
echo parseDoubleQuote('\"fweihfekf\"');*/
function parseDoubleQuote($s) {
if(parseNullValue($s)) return "NULL";
$res = str_replace('"', '\"', $s);
$res = str_replace('\\\\"', '\"', $res);
//return $res;
return '"'.$res.'"';
}
function parseDoubleQuoteHas($s) {
if(parseNullValue($s)) return "NULL";
$res = str_replace('"', '\"', $s);
$res = str_replace('\\\\"', '\"', $res);
return '"'.$res.'"';
}
/*echo(getDateFromYear("[du borbel 1970's]]")."<br/>");
echo(getDateFromYear("1897")."<br/>");
echo(getDateFromYear("July 17 1897")."<br/>"); //1897 too early for strtotime -> keep full date ?
echo(getDateFromYear("1898-11-05")."<br/>"); //1897 too early -> keep full date ??
echo(getDateFromYear("189811-05")."<br/>"); //1897 too early -> keep full date ?*/
function getDateFromYear($year) {
if(parseNullValue($year)) return "NULL";
// remove any char, ], [, ', ", whitespace
// should get 4 digits group
//$res = preg_replace("/\b[a-zA-Z]*]*\'*\"*\[*[\s]*[0-9]{2}[\s]*\b/" , "", $year);
$res = preg_replace("/[^\d\s-]/" , "", $year);
$res1 = preg_split("/[\s-]/" , $res);
//var_dump($res1);
for ($i = 0; $i < sizeof($res1); $i++) {
if(strlen($res1[$i])==4) {
return $res1[$i];
}
}
$month=1; $day=1;
$hour=0; $minute=0; $second=0;
// no date with 4 digits
return "0";
}
function getInt($i) {
if(parseNullValue($i)) return "NULL";
// suppress [ ] char
$i = preg_replace("/\[*\]*/" ,"", $i);
return $i;
}
// return last index used in csv - useful for assigning id
function getLastIndex($file) {
$index;
while((!feof($file)) && ($val = fgetcsv($file))){
$index = $val[0];
}
return (empty($index)) ? 0 : $index+1;
}
// return words separated by delimiter
function parseNames($s, $delimiter=";"){
// get rid of first and last double quotes
$string = substr($s, 1, -1);
$array = explode($delimiter, $string);
for($i = 0; $i< sizeof($array); $i++){
$array[$i] = ltrim($array[$i]);
}
return array_filter($array, function($value) { return $value !== ''; });
}
// return true if $s in contained in $csv file (opened) (at position $pos), false otherwise
function isInCsv($file, $s, $pos){
rewind($file);
if(empty($s)) {
return false;
}
while(! feof($file)){
$val = fgetcsv($file);
if($val[$pos]==$s) {
return $val[0];
}
}
return false;
}
// return true if $s in contained in $csv file (opened) (at position $pos), false otherwise
// @TODO parse "?"
function isInCsvName($file, $s, $pos){
rewind($file);
if(empty($s)) {
return false;
}
$string = parseToCompare($s);
while(! feof($file)){
$content = fgetcsv($file);
$val = parseToCompare($content[$pos]);
if($val==$string) {
return $content[0];
}
}
return false;
}
// modify string so that it can match even with the following differences : whitespaces, dot, dash (essentially for names)
// in csv -> will keep first occurence
// @todo match entries when [as ...] is defined
function parseToCompare($s){
$res = preg_replace("/\s/", "", $s);
$res = preg_replace("/\-/", "", $res);
$res = preg_replace("/\./", "", $res);
$res = strtolower($res);
return $res;
}
/*
var_dump(parseComments("rfjidrgi ?"));
var_dump(parseComments("rfjidrgi (wewe)"));*/
// delete from $s all content between () or [] or ?
function parseComments($s) {
$res = preg_replace("/\[.*\]/", "", $s);
$res = preg_replace("/\(.*\)/", "", $res);
$res = preg_replace("/\?/", "", $res);
if(empty(preg_replace("/\s/","",$res))){
return "NULL";
}
$res = trim($res);
return $res;
}