English 中文(简体)
格式电话号码
原标题:Formatting phone number
  • 时间:2009-10-06 10:47:41
  •  标签:

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 will 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.

职能是:

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 看一算法,将用户输入电话号码的不同方式格式改为一个格式:+31(0)61 2345678

问题回答

我确信,除了确定国家法典之外,这方面确实有一个国际标准。 关于国际电话号码格式的实例,见here。 格式将根据国家以及类型变化。

e. 例如,在联合王国,我们有+44 (0)20 1234 1234,用于陆地线路;+44(0)7700 123456用于移动设备。

因此,如果你想适当做到这一点,我认为你需要确定国家法典,然后用国别格式pl。

此时此刻,我没有最后的解决办法,但愿指出,如果你不想仅仅分析荷兰的数字,会有一些困难:

  • 你们必须了解所有国际规范(有些是北美一+1的一位数,有些是荷兰的一位+31的两位数,有些是捷克的一位+420的三位数),如果国家法典与地区法典之间没有空间,那么你不能简单地确定空间的正确立场。

  • 此外,你不能自动假定,该地区法典中领先的零是多余的。 在像意大利这样的一些国家,甚至在国际呼吁中也必须使用头零,也就是说,如果你要从荷兰把Milan数字02 123456称作+39 02 123456,而不是+39 2 123456。

因此,它不仅涉及撰写简短的格式算法,而且涉及了解不同国家的不同形式和做法。

当然,你可以将荷兰的数字(我假定你完全了解和理解荷兰的电话号码格式)转换为描述的格式,并且通过仅仅拆除所有类型的白天空间,用“+”取代“0”——如果你不希望你能做很多工作的话。

这令人感兴趣,因为荷兰采用混合型移动号码(从06号开始)并非标准化方式。

或许,你应该仅仅写上这一试验驱动的文字。 很多令人感兴趣的案例是你想要掩盖的,因此,你至少能够显示你做些什么和你不做什么。

您可以利用这一职能:

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"




相关问题
热门标签