3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $arr_postcode_regexes = [ 'offshore' => [ ['A', 'B', '3', '[0-8]'], // AB30-38 ['A', 'B', '4', '[1-9]'], // AB41-49 ['A', 'B', '5', '[0-6]'], // AB50-56 ['F', 'K', '1', '[789]'], // FK17-19 ['F', 'K', '2', '[01]'], // FK20-21 ['G', '8', '3'], // G83 ['G', 'Y', '9'], // GY9 ['H', 'S', '[1-9]'], // HS1-9 ['I', 'M', '[0-9]', '[0-9A-Z]?'], // IM ['I', 'M', '[0-9]', '[0-9A-Z]?'], // IV ['K', 'A', '2', '[89]'], // KA27-28 ['K', 'W', '[0-9]', '[0-9]?'], // KW0-99 ['P', 'A', '[2-9]', '[0-9]'], // PA20-99 ['P', 'H', '1', '[5-9]'], // PH15-19 ['P', 'H', '[2-9]', '[0-9]'], // PH20-99 ['T', 'R', '2', '[1-5]'], // TR21-25 ['Z', 'E', '[0-9]', '[0-9A-Z]?'], // ZE ], 'ni' => [ ['B', 'T', '[0-9]', '[0-9A-Z]?'], // ZE ] ]; // Turn the readable-ish map above into actual regexes // It's important that all regexes match only full, valid postcodes, as prefix matching can be ambiguous. foreach ($arr_postcode_regexes as $str_id => $arr_parts) { $arr_temp = []; foreach ($arr_parts as $arr_part) { $arr_temp[] = implode('\s*', $arr_part) . '\s*[0-9]\s*[A-Z]\s*[A-Z]'; } $arr_postcode_regexes[$str_id] = '^\s*(' . implode('|', $arr_temp) . ')\s*$'; } $tests = [ 'M65GQ', 'G83GH', 'G833GH', 'IM14GF', 'TR207HG', 'TR217HG', 'TR21HG', 'BT12HG', ]; foreach ($tests as $test) { echo "$test:\n"; foreach ($arr_postcode_regexes as $name => $expr) { echo " $name: " . (preg_match('/' . $expr . '/i', $test) ? 'yes' : 'no') . "\n"; } echo "\n"; } var_dump($arr_postcode_regexes);
based on urL1v
Output for 5.4.0 - 7.1.0
M65GQ: offshore: no ni: no G83GH: offshore: no ni: no G833GH: offshore: yes ni: no IM14GF: offshore: yes ni: no TR207HG: offshore: no ni: no TR217HG: offshore: yes ni: no TR21HG: offshore: no ni: no BT12HG: offshore: no ni: yes array(2) { ["offshore"]=> string(738) "^\s*(A\s*B\s*3\s*[0-8]\s*[0-9]\s*[A-Z]\s*[A-Z]|A\s*B\s*4\s*[1-9]\s*[0-9]\s*[A-Z]\s*[A-Z]|A\s*B\s*5\s*[0-6]\s*[0-9]\s*[A-Z]\s*[A-Z]|F\s*K\s*1\s*[789]\s*[0-9]\s*[A-Z]\s*[A-Z]|F\s*K\s*2\s*[01]\s*[0-9]\s*[A-Z]\s*[A-Z]|G\s*8\s*3\s*[0-9]\s*[A-Z]\s*[A-Z]|G\s*Y\s*9\s*[0-9]\s*[A-Z]\s*[A-Z]|H\s*S\s*[1-9]\s*[0-9]\s*[A-Z]\s*[A-Z]|I\s*M\s*[0-9]\s*[0-9A-Z]?\s*[0-9]\s*[A-Z]\s*[A-Z]|I\s*M\s*[0-9]\s*[0-9A-Z]?\s*[0-9]\s*[A-Z]\s*[A-Z]|K\s*A\s*2\s*[89]\s*[0-9]\s*[A-Z]\s*[A-Z]|K\s*W\s*[0-9]\s*[0-9]?\s*[0-9]\s*[A-Z]\s*[A-Z]|P\s*A\s*[2-9]\s*[0-9]\s*[0-9]\s*[A-Z]\s*[A-Z]|P\s*H\s*1\s*[5-9]\s*[0-9]\s*[A-Z]\s*[A-Z]|P\s*H\s*[2-9]\s*[0-9]\s*[0-9]\s*[A-Z]\s*[A-Z]|T\s*R\s*2\s*[1-5]\s*[0-9]\s*[A-Z]\s*[A-Z]|Z\s*E\s*[0-9]\s*[0-9A-Z]?\s*[0-9]\s*[A-Z]\s*[A-Z])\s*$" ["ni"]=> string(59) "^\s*(B\s*T\s*[0-9]\s*[0-9A-Z]?\s*[0-9]\s*[A-Z]\s*[A-Z])\s*$" }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/N95Ks on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/N95Ks on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/N95Ks on line 3
Process exited with code 255.