PHP 电话号码格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 电话号码格式相关的知识,希望对你有一定的参考价值。

// Formats a phone number as (xxx) xxx-xxxx or xxx-xxxx depending on the length.
function format_phone($phone)
{
  $phone = preg_replace("/[^0-9]/", '', $phone);

  if (strlen($phone) == 7)
    return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
  elseif (strlen($phone) == 10)
    return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
  else
    return $phone;
}

以上是关于PHP 电话号码格式的主要内容,如果未能解决你的问题,请参考以下文章

php 格式化美国电话号码

php 格式化电话号码

PHP 将电话号码转换为格式标准

PHP 电话号码格式

PHP 格式化电话号码

php - preg_replace 突出显示格式化的电话号码