@ 2017-07-11T12:08:01Z <?php
function extractPatterns(array $rules) : array
{
$postcodes = [];
foreach ($rules as $rule) {
if (preg_match('/^([A-Z]{1,2})([0-9]{1,2})-([0-9]{1,2})$/', trim($rule), $matches)) {
for ($i = $matches[2]; $i <= $matches[3]; $i++) {
$postcodes[] = sprintf('%s%s', $matches[1], $i);
}
continue;
}
if (preg_match('([A-Z]{1,2}[0-9]{1,2})', $rule, $matches)) {
$postcodes[] = $matches[0];
continue;
}
if (preg_match('[A-Z]{1,2})', $rule, $matches)) {
$postcodes[] = $matches[0];
continue;
}
}
return $postcodes;
}
$tests = [
[
'rules' => 'AB41-56, FK19-21, NG12',
'expected' => [
'AB41',
'AB42',
'AB43',
'AB44',
'AB45',
'AB46',
'AB47',
'AB48',
'AB49',
'AB50',
'AB51',
'AB52',
'AB53',
'AB54',
'AB55',
'AB56',
'FK19',
'FK20',
'FK21',
'NG12'
]
],
[
'rules' => 'HU1, W1-5',
'expected' => [
'HU1',
'W1',
'W2',
'W3',
'W4',
'W5'
]
]
];
foreach ($tests as $test) {
assert(extractPatterns(explode(',', $test['rules'])) == $test['expected']);
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 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 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
preferences:dark mode live preview
168.8 ms | 402 KiB | 173 Q