3v4l.org

run code in 300+ PHP versions simultaneously
<?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*$'; } var_dump($arr_postcode_regexes);

preferences:
43.02 ms | 402 KiB | 5 Q