3v4l.org

run code in 300+ PHP versions simultaneously
<?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'])) == $rules['expected']); }
Output for 8.3.0 - 8.3.7
Warning: Undefined variable $rules in /in/ZFMhL on line 68 Warning: Trying to access array offset on null in /in/ZFMhL on line 68 Fatal error: Uncaught AssertionError: assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) in /in/ZFMhL:68 Stack trace: #0 /in/ZFMhL(68): assert(false, 'assert(extractP...') #1 {main} thrown in /in/ZFMhL on line 68
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Warning: Undefined variable $rules in /in/ZFMhL on line 68 Warning: Trying to access array offset on value of type null in /in/ZFMhL on line 68 Fatal error: Uncaught AssertionError: assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) in /in/ZFMhL:68 Stack trace: #0 /in/ZFMhL(68): assert(false, 'assert(extractP...') #1 {main} thrown in /in/ZFMhL on line 68
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Notice: Undefined variable: rules in /in/ZFMhL on line 68 Notice: Trying to access array offset on value of type null in /in/ZFMhL on line 68 Warning: assert(): assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) failed in /in/ZFMhL on line 68 Notice: Undefined variable: rules in /in/ZFMhL on line 68 Notice: Trying to access array offset on value of type null in /in/ZFMhL on line 68 Warning: assert(): assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) failed in /in/ZFMhL on line 68
Output for 7.3.32 - 7.3.33
Warning: assert(): assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) failed in /in/ZFMhL on line 68 Warning: assert(): assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) failed in /in/ZFMhL on line 68
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.31
Notice: Undefined variable: rules in /in/ZFMhL on line 68 Warning: assert(): assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) failed in /in/ZFMhL on line 68 Notice: Undefined variable: rules in /in/ZFMhL on line 68 Warning: assert(): assert(extractPatterns(explode(',', $test['rules'])) == $rules['expected']) failed in /in/ZFMhL on line 68

preferences:
123.19 ms | 403 KiB | 172 Q