1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Check if a string number starts with one ore more zero |
5
|
|
|
* i.e.: 00...000 or 000...0Xxxx.x with X an int |
6
|
|
|
* @param $value |
7
|
|
|
* @return bool |
8
|
|
|
*/ |
9
|
|
|
function isStringNumberStartsWithMoreThanOneZero($value) |
10
|
|
|
{ |
11
|
|
|
return preg_match('/^[0]{2,}$/', $value) === 1 || preg_match('/^0{1,}[1-9]{1,}$/', $value) === 1; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Check if the value (int, float or string) is a integer and greater than zero.. |
16
|
|
|
* Only number >0 and <=PHP_INT_MAX |
17
|
|
|
* or if $acceptIntegerFloatingPoints==true a floating point that match an positive integer). |
18
|
|
|
* @param $value |
19
|
|
|
* @param bool $acceptIntegerFloatingPoints |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
function isIntegerPositive($value, $acceptIntegerFloatingPoints = false) : bool |
23
|
|
|
{ |
24
|
|
|
return isInteger($value, true, $acceptIntegerFloatingPoints) && $value > 0; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if the value (int, float or string) is a integer and greater than zero or equals to zero.. |
29
|
|
|
* Only number >=0 and <=PHP_INT_MAX |
30
|
|
|
* or if $acceptIntegerFloatingPoints==true a floating point that match an positive integer). |
31
|
|
|
* @param $value |
32
|
|
|
* @param bool $acceptIntegerFloatingPoints |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
function isIntegerPositiveOrZero($value, $acceptIntegerFloatingPoints = false) : bool |
36
|
|
|
{ |
37
|
|
|
return isInteger($value, true, $acceptIntegerFloatingPoints) && $value >= 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if the value (int, float or string) is a integer. |
42
|
|
|
* Only number <=PHP_INT_MAX (and >=PHP_INT_MIN if unsigned=true) |
43
|
|
|
* or if $acceptIntegerFloatingPoints==true a floating point that match an integer). |
44
|
|
|
* @param $value |
45
|
|
|
* @param bool $unsigned |
46
|
|
|
* @param bool $acceptIntegerFloatingPoints |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
function isInteger($value, $unsigned = true, $acceptIntegerFloatingPoints = false) : bool |
50
|
|
|
{ |
51
|
|
|
if (isStringNumberStartsWithMoreThanOneZero($value)) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
//accept only integer number and if $acceptIntegerFloatingPoints is true accept integer floating point too. |
56
|
|
|
return ((preg_match('/^' . ($unsigned ? '' : '-{0,1}') . '[0-9]{1,}$/', $value) === 1 |
57
|
|
|
&& ($value <= PHP_INT_MAX && $value >= PHP_INT_MIN && (((int)$value) == $value)) |
58
|
|
|
) |
59
|
|
|
|| ($acceptIntegerFloatingPoints && isIntegerFloatingPoint($value, $unsigned))); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if string is a valid floating point that |
64
|
|
|
* match an integer (<=PHP_INT_MAX and >=PHP_INT_MIN if unsigned=true) |
65
|
|
|
* or is an integer |
66
|
|
|
* Ex.: 1, 1e2, 1E2, 1e+2, 1e-2, 1.4e+2, -1.2e+2, -1.231e-2 etc... |
67
|
|
|
* @param $value |
68
|
|
|
* @param bool $unsigned |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
function isIntegerFloatingPoint($value, $unsigned = true) : bool |
72
|
|
|
{ |
73
|
|
|
return isFloatingPoint($value, $unsigned) |
74
|
|
|
&& $value <= PHP_INT_MAX && $value >= PHP_INT_MIN |
75
|
|
|
//big number rouned to int aproximately! |
76
|
|
|
//big number change into exp format |
77
|
|
|
&& ((int)((double)$value) == $value || (int)$value == $value || strpos(strtoupper((string)$value), 'E') === false); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check if string is a valid floating point. |
82
|
|
|
* Ex.: [+-]1, [+-]1e2, [+-]1E2, [+-]1e+2, [+-]1e-2, [+-]1.43234e+2, -1.231e+2, -1.231e-2 etc... |
83
|
|
|
* @param $value |
84
|
|
|
* @param $unsigned |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
function isFloatingPoint($value, $unsigned) : bool |
88
|
|
|
{ |
89
|
|
|
if (isStringNumberStartsWithMoreThanOneZero($value)) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return preg_match('/^' . ($unsigned ? '[+]{0,1}' : '[-+]{0,1}') . '[0-9]{1,}(\.[0-9]{1,}){0,1}([Ee][+,-]{0,1}[0-9]{1,}){0,}$/', |
94
|
|
|
$value) === 1; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if the value are a double (integer or float in the form 1, 1.11...1. |
99
|
|
|
* @param $value |
100
|
|
|
* @param int $dec |
101
|
|
|
* @param bool $unsigned |
102
|
|
|
* @param bool $exactDec if set to true aspect number of dec exact to $dec, |
103
|
|
|
* otherwise $dec is max decimals accepted (0 decimals are also ok in this case). |
104
|
|
|
* if $dec is an empty string, accept 0 to infinite decimals. |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
function isDouble($value, $dec = 2, $unsigned = true, $exactDec = false) : bool |
108
|
|
|
{ |
109
|
|
|
if (isStringNumberStartsWithMoreThanOneZero($value)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
$regEx = '/^' . ($unsigned ? '' : '-{0,1}') . '[0-9]{1,}(\.{1}[0-9]{' . ($exactDec ? '' : '1,') . $dec . '})' . ($exactDec ? '{1}' : '{0,1}') . '$/'; |
113
|
|
|
return preg_match($regEx, $value) === 1; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if string is dd/mm/YYYY |
118
|
|
|
* @param $value |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
function isDateIta($value) : bool |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
if ($value === null || $value == '' || strlen($value) != 10 || strpos($value, '/') === false) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
list($dd, $mm, $yyyy) = explode('/', $value); |
127
|
|
|
try { |
128
|
|
|
return checkdate($mm, $dd, $yyyy); |
129
|
|
|
} catch (Exception $e) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Check if string is YYYY-mm-dd |
136
|
|
|
* @param $value |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
function isDateIso($value) : bool |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
if ($value === null || $value == '' || strlen($value) != 10 || strpos($value, '-') === false) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
list($yyyy, $mm, $dd) = explode('-', $value); |
145
|
|
|
try { |
146
|
|
|
return checkdate($mm, $dd, $yyyy); |
147
|
|
|
} catch (Exception $e) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Check if string is YYYY-mm-dd HH:ii:ss |
154
|
|
|
* @param $value |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
function isDateTimeIso($value) : bool |
158
|
|
|
{ |
159
|
|
|
if (!isDateIso(substr($value, 0, 10))) { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
return isTimeIso(substr($value, 11)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if string is dd/mm/YYYY HH:ii:ss |
167
|
|
|
* @param $value |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
function isDateTimeIta($value) : bool |
171
|
|
|
{ |
172
|
|
|
if (!isDateIta(substr($value, 0, 10))) { |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
return isTimeIso(substr($value, 11)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if string is HH:ii:ss |
180
|
|
|
* @param $value |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
function isTimeIso($value) : bool |
184
|
|
|
{ |
185
|
|
|
$strRegExp = '/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/'; |
186
|
|
|
return preg_match($strRegExp, $value) === 1; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* An alias of isTimeIso. |
191
|
|
|
* @param $value |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
function isTimeIta($value) |
195
|
|
|
{ |
196
|
|
|
return isTimeIso($value); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param $value |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
function isMail($value) : bool |
204
|
|
|
{ |
205
|
|
|
return !(filter_var($value, FILTER_VALIDATE_EMAIL) === false); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* isIPv4 check if is a valid IP v4 |
210
|
|
|
* @param string $IP2Check IP to check |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
function isIPv4($IP2Check) : bool |
214
|
|
|
{ |
215
|
|
|
return !(filter_var($IP2Check, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Simple Check if a URL address syntax is valid (this regular expression also allows dashes in the URL). |
220
|
|
|
* Do not support ful URI and allow only popular scheme (http|https|ftp|mailto|file|data). |
221
|
|
|
* Require scheme protocol or www. i.e.: http://dummy.com and www.dummy.com return true but dummy.com return false. |
222
|
|
|
* @param $url |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
function isUrl($url) : bool |
226
|
|
|
{ |
227
|
|
|
if (preg_match('/^(https?|ftp|mailto|file|data):\/\/\./i', $url) === 1) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
return preg_match('/\b(?:(?:https?|ftp|mailto|file|data):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i', |
231
|
|
|
$url) === 1; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Controlla partita IVA. |
236
|
|
|
* @author Umberto Salsi <[email protected]> |
237
|
|
|
* @version 2012-05-12 |
238
|
|
|
* @param string $pi Partita IVA costituita da 11 cifre. Non sono ammessi |
239
|
|
|
* caratteri di spazio, per cui i campi di input dell'utente dovrebbero |
240
|
|
|
* essere trimmati preventivamente. La stringa vuota e' ammessa, cioe' |
241
|
|
|
* il dato viene considerato opzionale. |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
|
|
function isPiva(string $pi) : bool |
245
|
|
|
{ |
246
|
|
|
if ($pi === null || $pi === '' || strlen($pi) != 11 || preg_match("/^[0-9]+\$/", $pi) != 1) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
$s = 0; |
250
|
|
|
for ($i = 0; $i <= 9; $i += 2) { |
251
|
|
|
$s += ord($pi[$i]) - ord('0'); |
252
|
|
|
} |
253
|
|
|
for ($i = 1; $i <= 9; $i += 2) { |
254
|
|
|
$c = 2 * (ord($pi[$i]) - ord('0')); |
255
|
|
|
if ($c > 9) { |
256
|
|
|
$c -= 9; |
257
|
|
|
} |
258
|
|
|
$s += $c; |
259
|
|
|
} |
260
|
|
|
return !((10 - $s % 10) % 10 != ord($pi[10]) - ord('0')); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Controlla codice fiscale. |
265
|
|
|
* @author Umberto Salsi <[email protected]> |
266
|
|
|
* @version 2012-05-12 |
267
|
|
|
* @param string $cf Codice fiscale costituito da 16 caratteri. Non |
268
|
|
|
* sono ammessi caratteri di spazio, per cui i campi di input dell'utente |
269
|
|
|
* dovrebbero essere trimmati preventivamente. La stringa vuota e' ammessa, |
270
|
|
|
* cioe' il dato viene considerato opzionale. |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
|
|
function isCf(string $cf) : bool |
274
|
|
|
{ |
275
|
|
|
if ($cf === null || $cf === '' || strlen($cf) != 16) { |
276
|
|
|
return false; |
277
|
|
|
} |
278
|
|
|
$cf = strtoupper($cf); |
279
|
|
|
if (preg_match("/^[A-Z0-9]+\$/", $cf) != 1) { |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
$s = 0; |
283
|
|
|
for ($i = 1; $i <= 13; $i += 2) { |
284
|
|
|
$c = $cf[$i]; |
285
|
|
|
if (strcmp($c, "0") >= 0 && strcmp($c, "9") <= 0) { |
286
|
|
|
$s += ord($c) - ord('0'); |
287
|
|
|
} else { |
288
|
|
|
$s += ord($c) - ord('A'); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
for ($i = 0; $i <= 14; $i += 2) { |
292
|
|
|
$c = $cf[$i]; |
293
|
|
|
switch ($c) { |
294
|
|
|
case '0': |
295
|
|
|
$s += 1; |
296
|
|
|
break; |
297
|
|
|
case '1': |
298
|
|
|
$s += 0; |
299
|
|
|
break; |
300
|
|
|
case '2': |
301
|
|
|
$s += 5; |
302
|
|
|
break; |
303
|
|
|
case '3': |
304
|
|
|
$s += 7; |
305
|
|
|
break; |
306
|
|
|
case '4': |
307
|
|
|
$s += 9; |
308
|
|
|
break; |
309
|
|
|
case '5': |
310
|
|
|
$s += 13; |
311
|
|
|
break; |
312
|
|
|
case '6': |
313
|
|
|
$s += 15; |
314
|
|
|
break; |
315
|
|
|
case '7': |
316
|
|
|
$s += 17; |
317
|
|
|
break; |
318
|
|
|
case '8': |
319
|
|
|
$s += 19; |
320
|
|
|
break; |
321
|
|
|
case '9': |
322
|
|
|
$s += 21; |
323
|
|
|
break; |
324
|
|
|
case 'A': |
325
|
|
|
$s += 1; |
326
|
|
|
break; |
327
|
|
|
case 'B': |
328
|
|
|
$s += 0; |
329
|
|
|
break; |
330
|
|
|
case 'C': |
331
|
|
|
$s += 5; |
332
|
|
|
break; |
333
|
|
|
case 'D': |
334
|
|
|
$s += 7; |
335
|
|
|
break; |
336
|
|
|
case 'E': |
337
|
|
|
$s += 9; |
338
|
|
|
break; |
339
|
|
|
case 'F': |
340
|
|
|
$s += 13; |
341
|
|
|
break; |
342
|
|
|
case 'G': |
343
|
|
|
$s += 15; |
344
|
|
|
break; |
345
|
|
|
case 'H': |
346
|
|
|
$s += 17; |
347
|
|
|
break; |
348
|
|
|
case 'I': |
349
|
|
|
$s += 19; |
350
|
|
|
break; |
351
|
|
|
case 'J': |
352
|
|
|
$s += 21; |
353
|
|
|
break; |
354
|
|
|
case 'K': |
355
|
|
|
$s += 2; |
356
|
|
|
break; |
357
|
|
|
case 'L': |
358
|
|
|
$s += 4; |
359
|
|
|
break; |
360
|
|
|
case 'M': |
361
|
|
|
$s += 18; |
362
|
|
|
break; |
363
|
|
|
case 'N': |
364
|
|
|
$s += 20; |
365
|
|
|
break; |
366
|
|
|
case 'O': |
367
|
|
|
$s += 11; |
368
|
|
|
break; |
369
|
|
|
case 'P': |
370
|
|
|
$s += 3; |
371
|
|
|
break; |
372
|
|
|
case 'Q': |
373
|
|
|
$s += 6; |
374
|
|
|
break; |
375
|
|
|
case 'R': |
376
|
|
|
$s += 8; |
377
|
|
|
break; |
378
|
|
|
case 'S': |
379
|
|
|
$s += 12; |
380
|
|
|
break; |
381
|
|
|
case 'T': |
382
|
|
|
$s += 14; |
383
|
|
|
break; |
384
|
|
|
case 'U': |
385
|
|
|
$s += 16; |
386
|
|
|
break; |
387
|
|
|
case 'V': |
388
|
|
|
$s += 10; |
389
|
|
|
break; |
390
|
|
|
case 'W': |
391
|
|
|
$s += 22; |
392
|
|
|
break; |
393
|
|
|
case 'X': |
394
|
|
|
$s += 25; |
395
|
|
|
break; |
396
|
|
|
case 'Y': |
397
|
|
|
$s += 24; |
398
|
|
|
break; |
399
|
|
|
case 'Z': |
400
|
|
|
$s += 23; |
401
|
|
|
break; |
402
|
|
|
/*. missing_default: .*/ |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
return !(chr($s % 26 + ord('A')) != $cf[15]); |
406
|
|
|
} |
407
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.