Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Last active March 25, 2023 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/2370e24c4882abc83773866871356f88 to your computer and use it in GitHub Desktop.
Save danielhaim1/2370e24c4882abc83773866871356f88 to your computer and use it in GitHub Desktop.
This function formats UK phone numbers, adds missing national trunk codes, and returns an error message if invalid.
/**
format_uk_phone_number - A function to format UK phone numbers
This function takes in a phone number string and formats it according to UK phone number conventions.
@param {string} phone_number - The phone number to be formatted.
@returns {string} The formatted UK phone number string, or "Invalid UK phone number" if the input is invalid.
**/
function format_uk_phone_number(phone_number) {
// Remove any non-digit characters
phone_number = phone_number.replace(/\D/g, '');
// Check the length of the input string
let len = phone_number.length;
if (len < 3 || (len >= 12 && phone_number.substring(0, 3) !== "+44")) {
return "Invalid UK phone number";
}
// Check if the phone number includes the national trunk code (i.e. starts with "0").
if (phone_number[0] !== "0") {
phone_number = "0" + phone_number;
}
let code = phone_number.substring(0, 2);
if (code === "01" || code === "02") {
let area_codes = {
"020": [3, 4, 4], // London
"023": [3, 4, 4], // Southampton
"024": [3, 4, 4], // Coventry
"028": [3, 4, 4], // Northern Ireland
"029": [3, 4, 4], // Cardiff
};
let formatted = "";
let area_code = phone_number.substring(0, 3);
if (area_codes.hasOwnProperty(area_code)) {
let split = area_codes[area_code];
formatted = phone_number.substring(0, split[0]) + " " + phone_number.substring(split[0], split[0] + split[1]) + " " + phone_number.substring(split[0] + split[1], split[0] + split[1] + split[2]);
} else {
formatted = phone_number.substring(0, 5) + " " + phone_number.substring(5, 11);
}
return formatted;
}
if (code === "03") {
return phone_number.substring(0, 4) + " " + phone_number.substring(4, 7) + " " + phone_number.substring(7);
}
if (["05", "06", "07", "08"].includes(code)) {
// Non-geographic and premium rate numbers
if (len == 7 || len == 8) {
if (phone_number.substring(0, 4) == "0800") {
return phone_number.substring(0, 4) + " " + phone_number.substring(4);
} else {
return phone_number.substring(0, 3) + " " + phone_number.substring(3);
}
}
if (len == 10) {
return phone_number.substring(0, 4) + " " + phone_number.substring(4, 7) + " " + phone_number.substring(7);
}
if (len == 11) {
return phone_number.substring(0, 5) + " " + phone_number.substring(5, 8) + " " + phone_number.substring(8);
}
}
if (len <= 6) {
return phone_number;
}
return "Invalid UK " + len + " " + phone_number;
}
<?php
function format_uk_phone_number($phone_number) {
// Remove any non-digit characters
$phone_number = preg_replace('/\D/', '', $phone_number);
// Check the length of the input string
$len = strlen($phone_number);
if ($len < 3 || ($len >= 12 && substr($phone_number, 0, 3) !== "+44")) {
return "Invalid UK phone number";
}
// Check if the phone number includes the national trunk code (i.e. starts with "0").
if ($phone_number[0] !== "0") {
$phone_number = "0" . $phone_number;
}
$code = substr($phone_number, 0, 2);
if ($code === "01" || $code === "02") {
$area_codes = [
"020" => [3, 4, 4], // London
"023" => [3, 4, 4], // Southampton
"024" => [3, 4, 4], // Coventry
"028" => [3, 4, 4], // Northern Ireland
"029" => [3, 4, 4], // Cardiff
];
$formatted = "";
$area_code = substr($phone_number, 0, 3);
if (isset($area_codes[$area_code])) {
$split = $area_codes[$area_code];
$formatted = substr($phone_number, 0, $split[0]) . " " . substr($phone_number, $split[0], $split[1]) . " " . substr($phone_number, $split[0] + $split[1], $split[2]);
} else {
$formatted = substr($phone_number, 0, 5) . " " . substr($phone_number, 5, 6);
}
return $formatted;
}
if ($code === "03") {
return substr($phone_number, 0, 4) . " " . substr($phone_number, 4, 3) . " " . substr($phone_number, 7);
}
if (in_array($code, ["05", "06", "07", "08"])) {
// Non-geographic and premium rate numbers
if ($len == 7 || $len == 8) {
if (substr($phone_number, 0, 4) == "0800") {
return substr($phone_number, 0, 4) . " " . substr($phone_number, 4);
} else {
return substr($phone_number, 0, 3) . " " . substr($phone_number, 3);
}
}
if ($len == 10) {
return substr($phone_number, 0, 4) . " " . substr($phone_number, 4, 3) . " " . substr($phone_number, 7);
}
if ($len == 11) {
return substr($phone_number, 0, 5) . " " . substr($phone_number, 5, 3) . " " . substr($phone_number, 8);
}
}
if ($len <= 6) {
return $phone_number;
}
return "Invalid UK " . $len . " " . $phone_number;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment