<?php
$records = [
['1', '2'],
['3', '9'],
['12', '8' ],
['20', '2' ],
['22', '15' ],
['37', '16' ],
['53', '16' ],
['69', '16' ],
['85', '1' ],
['86', '4' ],
['90', '4' ],
['94', '4' ],
['98', '4' ],
['102', '4', '5' ],
['106+X', 'X'],
['106+X', '5', '10'],
['111+X', 'Y'],
['111+X+Y', '7', '20'],
['118+X+Y', 'Z'],
];
$X = 10;
$Y = 15;
$Z = 5;
foreach ($records as $posLen) {
$pos = $posLen[0];
$len = $posLen[1];
// $val = $posLen[2];
// if $len is a character, then it represents a variable.
if (!is_numeric($len)) {
$len = $$len;
}
$finalPos = 0;
$posArray = explode('+', $pos);
foreach($posArray as $posItem) {
if (is_numeric($posItem)) {
$finalPos += $posItem;
} else {
$finalPos += $$posItem;
}
}
echo "$finalPos, $len" . PHP_EOL;
}
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- 1, 2
3, 9
12, 8
20, 2
22, 15
37, 16
53, 16
69, 16
85, 1
86, 4
90, 4
94, 4
98, 4
102, 4
116, 10
116, 5
121, 15
136, 7
143, 5
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
1, 2
3, 9
12, 8
20, 2
22, 15
37, 16
53, 16
69, 16
85, 1
86, 4
90, 4
94, 4
98, 4
102, 4
116, 10
116, 5
121, 15
136, 7
143, 5
preferences:
254.63 ms | 402 KiB | 291 Q