-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOgerFunc.class.php
More file actions
245 lines (203 loc) · 6.37 KB
/
OgerFunc.class.php
File metadata and controls
245 lines (203 loc) · 6.37 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/**
* some helpfull functions
*/
class OgerFunc {
/**
* untabify string with tabstop width
*/
public static function untabify($string, $tabWidth = 8) {
$parts = explode("\t", $string);
$result = array_shift($parts);
while (count($parts)) {
// the previous part is followed by at least one blank
$result .= ' ';
// insert blanks till the next tabstop
while (strlen($result) % $tabWidth) {
$result .= ' ';
}
// append next string part
$result .= array_shift($parts);
} // loop over parts
return $result;
} // end of untabify string
/**
* Check if array is associative.
* That means if it has non numeric keys.
* ATTENTION: Associative arrays with only numeric keys are
* treated as NOT associative!!!! (but this is a general problem
* also in php internal-functions like array_merge, ...)
*/
public static function isAssoc($array) {
if (!is_array($array)) {
return false;
}
foreach ($array as $key => $value) {
if (!is_numeric($key)) {
return true;
}
}
return false;
} // eo assoc check
/**
* Insert all key/value pairs of an associative array into another associative array after specified key.
* Numeric arrays are NOT handled properly!
* WE DONT CHECK ANYTHING! Values are overwritten and other unexpected results may happen if input is not correct.
* Values from array2 may overwrites values of array1 if the same key is present in both arrays.
* TODO: Check: Maybe this is slow. There is another solution spliting the original
* array into keys and values, insert the new keys and values via array_splice and
* create the result array via array_combine. See: <http://www.php.net/manual/en/function.array-splice.php>
* @array1: Associative array.
* @searchKey: Key after which array 2 is inserted.
* @array2: Associative array.
*/
public static function arrayInsertAfterKey(&$array1, $searchKey, $array2) {
$array = array();
$insertDone = false;
foreach ($array1 as $key1 => $value1) {
$array[$key1] = $value1;
if ($key1 == $searchKey) {
foreach ($array2 as $key2 => $value2) {
$array[$key2] = $value2;
}
$insertDone = true;
}
}
// if we did not find the key then append the inserts here
if (!$insertDone) {
foreach ($array2 as $key2 => $value2) {
$array[$key2] = $value2;
}
}
$array1 = $array;
return $array;
} // eo insert after key
/**
* Insert all key/value pairs of an associative array into another associative array before specified key.
* Numeric arrays are NOT handled properly!
* WE DONT CHECK ANYTHING! Values are overwritten and other unexpected results may happen if input is not correct.
* Values from array2 may overwrites values of array1 if the same key is present in both arrays.
* TODO: Check: Maybe this is slow. There is another solution spliting the original
* array into keys and values, insert the new keys and values via array_splice and
* create the result array via array_combine. See: <http://www.php.net/manual/en/function.array-splice.php>
* @array1: Associative array.
* @searchKey: Key after which array 2 is inserted.
* @array2: Associative array.
*/
public static function arrayInsertBeforeKey(&$array1, $searchKey, $array2) {
$array = array();
$insertDone = false;
foreach ($array1 as $key1 => $value1) {
if ($key1 == $searchKey) {
foreach ($array2 as $key2 => $value2) {
$array[$key2] = $value2;
}
$insertDone = true;
}
$array[$key1] = $value1;
}
// if we did not find the key then append the inserts here
if (!$insertDone) {
foreach ($array2 as $key2 => $value2) {
$array[$key2] = $value2;
}
}
$array1 = $array;
return $array;
} // eo insert before key
/**
* Evaluate an arithmetic expressoin from string.
* Only base arithmetic works because of security reasons.
*/
public static function evalMath($str) {
$str = preg_replace('/[^0-9\. \+\-\*\/\(\)]+/', '', $str);
return eval('return ' . $str . ';');
}
/**
* Indents a flat JSON string to make it more human-readable.
* from: <http://recursive-design.com/blog/2008/03/11/format-json-with-php/>
* @param string $json The original JSON string to process.
* @return string Indented version of the original JSON string.
*/
public static function beautifyJson($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i=0; $i<=$strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
for ($j=0; $j<$pos; $j++) {
$result .= $indentStr;
}
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
} // eo beautify json
/**
* Merge two or more arrays and preserve numeric keys
*/
public static function arrayMergeAssoc() {
$result = array();
$arrays = func_get_args();
foreach($arrays as $array) {
if ($array == null) {
continue;
}
foreach($array as $key => $value) {
$result[$key] = $value;
}
}
return $result;
} // eo array merge assoc
/**
* Explode an string and create assiciative array
*/
public static function explodeAssoc($separator, $str, $limit = 0) {
$result = array();
if (func_num_args() > 2) {
$array = explode($separator, $str, $limit);
}
else {
$array = explode($separator, $str);
}
foreach($array as $key => $value) {
$result[$value] = $value;
}
return $result;
} // eo explode assoc
} // eo class
?>