For a decent and standardized php function I want to format a user inputted string to a std. international phone number format. The string will be stripped from other characters as digits and +-signs. All spaces wil开发者_JAVA技巧l be deleted. After that I want to format the different strings to a standardized form:
+310612345678 -> +31 (0)61 234 5678
00310612345678 -> +31 (0)61 234 5678
+31612345678 -> +31 (0)61 234 5678
//add a leading zero between brackets (0))
0031612345678 -> +31 (0)61 234 5678
06 12345678 -> +31 (0)61 234 5678
//When no country code is given (no 00xx or +xx) than add the dutch one.
This is the function which i have allready:
function formatPhoneNumber($num){
//delete everything except digits and +
$number = preg_replace('/[^\d|\+]/', '', $number);
if(substr($number,0,1) == '+' || substr($number, 0,2) == 00){
//
}else{
$number = "+31".$number;
}
/////////
// formatting as mentioned above
/////////
return $num
}
I'm Looking for a algorithm to format the different ways of user inputted telephone numbers to one format: +31 (0)61 2345678
I'm not sure there's really an international standard for this beyond prefixing the country code. For examples of international phone number formatting, see here. Formatting will change depending on the country, and also on the type.
e.g. in the UK we have +44 (0)20 1234 1234 for landlines, and +44 (0)7700 123456 for mobiles.
Consequently, if you want to do this properly, I think you'll need to identify the country code, and then plug in a country-specific formatter.
I've no final solution for this in the moment but would like to point out some difficulties if you don't want to analyze only Dutch numbers:
You have to know all international prefixes (some are one-digit like the North-American one +1, some are two-digit like the Dutch one +31, and some are three-digit like the Czech one +420) and if there is no space between country code and area code you cannot simply identify the correct position for the space in your format.
Further you cannot assume automatically that a leading zero in the area code is redundant. In some countries like Italy the leading zero has to be used even in international calls, i. e. if you want to call the Milan numer 02 123456 from the Netherlands you have to call +39 02 123456 instead of +39 2 123456.
So, it's not just about writing a short formatting algorithm but very much about understanding the different number formats and usages in different countries.
Of course you can convert Dutch numbers (I assume you exactly know and understand the Dutch phone number format) to the described format and convert every foreign number by just removing all whitespaces of any kind and replace a leading "00" with a "+"--if you want no quick-and-dirty solution you have much work to do.
That's interesting, as it is not the standardized way to format dutch mobile numbers (all start with 06) in the Netherlands.
You should probably just write this test-driven. There are lots of interesting cases you'll want to cover, and that way you'll at least be able to show what you do and what you do not convert.
You can use this function:
function maskPhoneNumber(num, mask) {
let regex = ''
let i = 0
const format = mask.replace(/(#+)/g, (match) => {
regex += `([\+\\d]{${match.length}})`
i ++
return `$${i}`
})
return num.replace(new RegExp(regex), format).substr(0, mask.length)
}
console.error(maskPhoneNumber('+310612345678', '### (#)## ### ####'))
// "+31 (0)61 234 5678"
精彩评论