-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathini_parse_quantity_polyfill.php
More file actions
54 lines (53 loc) · 1.21 KB
/
Copy pathini_parse_quantity_polyfill.php
File metadata and controls
54 lines (53 loc) · 1.21 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
<?php
if(!function_exists('ini_parse_quantity')) {
/**
* Very basic polyfill for ini_parse_quantity
*
* @param string $shorthand The quantity string to parse. It may include unit suffixes or numerical prefixes used for bases (e.g., hex, binary).
* @return int The parsed quantity in bytes as an integer value.
* @author Jack Dunn <https://github.com/JackDunnCodes>
* @license MPL-2.0
*/
function ini_parse_quantity($shorthand) {
$regex = '/^(0?[xXbBoO]?)([0-9A-Fa-f]*)([kKmMgG]?)$/';
preg_match($regex, $shorthand, $matches);
if(empty($matches)) {
trigger_error('Can\'t parse '.$shorthand.' :(', E_USER_WARNING);
return 0;
}
[ $full, $binOctHex, $value, $unit ] = $matches;
switch ($binOctHex) {
case '0b':
case '0B':
$base = 2;
break;
case '0o':
case '0O':
case '0':
$base = 8;
break;
case '0x':
case '0X':
$base = 16;
break;
default:
$base = 10;
break;
}
$value = intval($value, $base);
switch ($unit) {
case 'g':
case 'G':
$value *= 1024;
case 'm':
case 'M':
$value *= 1024;
case 'k':
case 'K':
$value *= 1024;
default:
break;
}
return $value;
}
}