在 PHP 中格式化电话号码

发布于 2024-10-12 17:37:22 字数 231 浏览 7 评论 0原文

我正在开发一个短信应用程序,需要能够将发件人的电话号码从 +11234567890 转换为 123-456-7890,以便可以与MySQL 数据库中的记录进行比较。

这些数字以后一种格式存储,以便在网站的其他地方使用,我宁愿不更改该格式,因为它需要修改大量代码。

我该如何使用 PHP 来解决这个问题?

谢谢!

I am working on an SMS app and need to be able to convert the sender's phone number from +11234567890 to 123-456-7890 so it can be compared to records in a MySQL database.

The numbers are stored in the latter format for use elsewhere on the site and I would rather not change that format as it would require modifying a lot of code.

How would I go about this with PHP?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

笛声青案梦长安 2024-10-19 17:37:22

这是一个美国电话格式化程序,适用于比当前答案更多版本的号码。

$numbers = explode("\n", '(111) 222-3333
((111) 222-3333
1112223333
111 222-3333
111-222-3333
(111)2223333
+11234567890
    1-8002353551
    123-456-7890   -Hello!
+1 - 1234567890 
');


foreach($numbers as $number)
{
    print preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
}

以下是正则表达式的细分:

Cell: +1 999-(555 0001)

.*          zero or more of anything "Cell: +1 "
(\d{3})     three digits "999"
[^\d]{0,7}  zero or up to 7 of something not a digit "-("
(\d{3})     three digits "555"
[^\d]{0,7}  zero or up to 7 of something not a digit " "
(\d{4})     four digits "0001"
.*          zero or more of anything ")"

更新:2015 年 3 月 11 日使用 {0,7} 而不是 {,7}

This is a US phone formatter that works on more versions of numbers than any of the current answers.

$numbers = explode("\n", '(111) 222-3333
((111) 222-3333
1112223333
111 222-3333
111-222-3333
(111)2223333
+11234567890
    1-8002353551
    123-456-7890   -Hello!
+1 - 1234567890 
');


foreach($numbers as $number)
{
    print preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
}

And here is a breakdown of the regex:

Cell: +1 999-(555 0001)

.*          zero or more of anything "Cell: +1 "
(\d{3})     three digits "999"
[^\d]{0,7}  zero or up to 7 of something not a digit "-("
(\d{3})     three digits "555"
[^\d]{0,7}  zero or up to 7 of something not a digit " "
(\d{4})     four digits "0001"
.*          zero or more of anything ")"

Updated: March 11, 2015 to use {0,7} instead of {,7}

紫轩蝶泪 2024-10-19 17:37:22
$data = '+11234567890';

if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data,  $matches ) )
{
    $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
    return $result;
}
$data = '+11234567890';

if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data,  $matches ) )
{
    $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
    return $result;
}
沦落红尘 2024-10-19 17:37:22

此功能将格式化国际(10 位以上数字)、非国际(10 位数字)或老式(7 位数字)电话号码。 10+、10 或 7 位数字以外的任何数字都将保持未格式化。

function formatPhoneNumber($phoneNumber) {
    $phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);

    if(strlen($phoneNumber) > 10) {
        $countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
        $areaCode = substr($phoneNumber, -10, 3);
        $nextThree = substr($phoneNumber, -7, 3);
        $lastFour = substr($phoneNumber, -4, 4);

        $phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
    }
    else if(strlen($phoneNumber) == 10) {
        $areaCode = substr($phoneNumber, 0, 3);
        $nextThree = substr($phoneNumber, 3, 3);
        $lastFour = substr($phoneNumber, 6, 4);

        $phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
    }
    else if(strlen($phoneNumber) == 7) {
        $nextThree = substr($phoneNumber, 0, 3);
        $lastFour = substr($phoneNumber, 3, 4);

        $phoneNumber = $nextThree.'-'.$lastFour;
    }

    return $phoneNumber;
}

This function will format international (10+ digit), non-international (10 digit) or old school (7 digit) phone numbers. Any numbers other than 10+, 10 or 7 digits will remain unformatted.

function formatPhoneNumber($phoneNumber) {
    $phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);

    if(strlen($phoneNumber) > 10) {
        $countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
        $areaCode = substr($phoneNumber, -10, 3);
        $nextThree = substr($phoneNumber, -7, 3);
        $lastFour = substr($phoneNumber, -4, 4);

        $phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
    }
    else if(strlen($phoneNumber) == 10) {
        $areaCode = substr($phoneNumber, 0, 3);
        $nextThree = substr($phoneNumber, 3, 3);
        $lastFour = substr($phoneNumber, 6, 4);

        $phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
    }
    else if(strlen($phoneNumber) == 7) {
        $nextThree = substr($phoneNumber, 0, 3);
        $lastFour = substr($phoneNumber, 3, 4);

        $phoneNumber = $nextThree.'-'.$lastFour;
    }

    return $phoneNumber;
}
毁梦 2024-10-19 17:37:22

假设您的电话号码始终具有这种精确的格式,您可以使用以下代码段:

$from = "+11234567890";
$to = sprintf("%s-%s-%s",
              substr($from, 2, 3),
              substr($from, 5, 3),
              substr($from, 8));

Assuming that your phone numbers always have this exact format, you can use this snippet:

$from = "+11234567890";
$to = sprintf("%s-%s-%s",
              substr($from, 2, 3),
              substr($from, 5, 3),
              substr($from, 8));
一口甜 2024-10-19 17:37:22

电话号码很难。对于更强大的国际解决方案,我推荐这个 维护良好的 PHP Google libphonenumber 库的端口。

像这样使用它,

use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;

$phoneUtil = PhoneNumberUtil::getInstance();

$numberString = "+12123456789";

try {
    $numberPrototype = $phoneUtil->parse($numberString, "US");

    echo "Input: " .          $numberString . "\n";
    echo "isValid: " .       ($phoneUtil->isValidNumber($numberPrototype) ? "true" : "false") . "\n";
    echo "E164: " .           $phoneUtil->format($numberPrototype, PhoneNumberFormat::E164) . "\n";
    echo "National: " .       $phoneUtil->format($numberPrototype, PhoneNumberFormat::NATIONAL) . "\n";
    echo "International: " .  $phoneUtil->format($numberPrototype, PhoneNumberFormat::INTERNATIONAL) . "\n";
} catch (NumberParseException $e) {
    // handle any errors
}

您将得到以下输出:

Input: +12123456789
isValid: true
E164: +12123456789
National: (212) 345-6789
International: +1 212-345-6789

我建议使用 E164 格式进行重复检查。您还可以检查该号码是否是实际的手机号码(使用 PhoneNumberUtil::getNumberType()),或者是否是美国号码(使用 PhoneNumberUtil::getRegionCodeForNumber()< /代码>)。

作为奖励,该库几乎可以处理任何输入。例如,如果您选择通过上面的代码运行 1-800-JETBLUE,您将获得

Input: 1-800-JETBLUE
isValid: true
E164: +18005382583
National: (800) 538-2583
International: +1 800-538-2583

Neato。

它对于美国以外的国家也同样有效。只需在 parse() 参数中使用另一个 ISO 国家/地区代码即可。

Phone numbers are hard. For a more robust, international solution, I would recommend this well-maintained PHP port of Google's libphonenumber library.

Using it like this,

use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;

$phoneUtil = PhoneNumberUtil::getInstance();

$numberString = "+12123456789";

try {
    $numberPrototype = $phoneUtil->parse($numberString, "US");

    echo "Input: " .          $numberString . "\n";
    echo "isValid: " .       ($phoneUtil->isValidNumber($numberPrototype) ? "true" : "false") . "\n";
    echo "E164: " .           $phoneUtil->format($numberPrototype, PhoneNumberFormat::E164) . "\n";
    echo "National: " .       $phoneUtil->format($numberPrototype, PhoneNumberFormat::NATIONAL) . "\n";
    echo "International: " .  $phoneUtil->format($numberPrototype, PhoneNumberFormat::INTERNATIONAL) . "\n";
} catch (NumberParseException $e) {
    // handle any errors
}

you will get the following output:

Input: +12123456789
isValid: true
E164: +12123456789
National: (212) 345-6789
International: +1 212-345-6789

I'd recommend using the E164 format for duplicate checks. You could also check whether the number is a actually mobile number or not (using PhoneNumberUtil::getNumberType()), or whether it's even a US number (using PhoneNumberUtil::getRegionCodeForNumber()).

As a bonus, the library can handle pretty much any input. If you, for instance, choose to run 1-800-JETBLUE through the code above, you will get

Input: 1-800-JETBLUE
isValid: true
E164: +18005382583
National: (800) 538-2583
International: +1 800-538-2583

Neato.

It works just as nicely for countries other than the US. Just use another ISO country code in the parse() argument.

帅气尐潴 2024-10-19 17:37:22

它比正则表达式更快。<​​/strong>

$input = "0987654321"; 

$output = substr($input, -10, -7) . "-" . substr($input, -7, -4) . "-" . substr($input, -4); 
echo $output;

It's faster than RegEx.

$input = "0987654321"; 

$output = substr($input, -10, -7) . "-" . substr($input, -7, -4) . "-" . substr($input, -4); 
echo $output;
丘比特射中我 2024-10-19 17:37:22

这是我的仅限美国的解决方案,其中区号作为可选组件、扩展名所需的分隔符以及正则表达式注释:

function formatPhoneNumber($s) {
$rx = "/
    (1)?\D*     # optional country code
    (\d{3})?\D* # optional area code
    (\d{3})\D*  # first three
    (\d{4})     # last four
    (?:\D+|$)   # extension delimiter or EOL
    (\d*)       # optional extension
/x";
preg_match($rx, $s, $matches);
if(!isset($matches[0])) return false;

$country = $matches[1];
$area = $matches[2];
$three = $matches[3];
$four = $matches[4];
$ext = $matches[5];

$out = "$three-$four";
if(!empty($area)) $out = "$area-$out";
if(!empty($country)) $out = "+$country-$out";
if(!empty($ext)) $out .= "x$ext";

// check that no digits were truncated
// if (preg_replace('/\D/', '', $s) != preg_replace('/\D/', '', $out)) return false;
return $out;
}

这是测试它的脚本:

$numbers = [
'3334444',
'2223334444',
'12223334444',
'12223334444x5555',
'333-4444',
'(222)333-4444',
'+1 222-333-4444',
'1-222-333-4444ext555',
'cell: (222) 333-4444',
'(222) 333-4444 (cell)',
];

foreach($numbers as $number) {
    print(formatPhoneNumber($number)."<br>\r\n");
}

Here's my USA-only solution, with the area code as an optional component, required delimiter for the extension, and regex comments:

function formatPhoneNumber($s) {
$rx = "/
    (1)?\D*     # optional country code
    (\d{3})?\D* # optional area code
    (\d{3})\D*  # first three
    (\d{4})     # last four
    (?:\D+|$)   # extension delimiter or EOL
    (\d*)       # optional extension
/x";
preg_match($rx, $s, $matches);
if(!isset($matches[0])) return false;

$country = $matches[1];
$area = $matches[2];
$three = $matches[3];
$four = $matches[4];
$ext = $matches[5];

$out = "$three-$four";
if(!empty($area)) $out = "$area-$out";
if(!empty($country)) $out = "+$country-$out";
if(!empty($ext)) $out .= "x$ext";

// check that no digits were truncated
// if (preg_replace('/\D/', '', $s) != preg_replace('/\D/', '', $out)) return false;
return $out;
}

And here's the script to test it:

$numbers = [
'3334444',
'2223334444',
'12223334444',
'12223334444x5555',
'333-4444',
'(222)333-4444',
'+1 222-333-4444',
'1-222-333-4444ext555',
'cell: (222) 333-4444',
'(222) 333-4444 (cell)',
];

foreach($numbers as $number) {
    print(formatPhoneNumber($number)."<br>\r\n");
}
我不吻晚风 2024-10-19 17:37:22

这是我的看法:

$phone='+11234567890';
$parts=sscanf($phone,'%2c%3c%3c%4c');
print "$parts[1]-$parts[2]-$parts[3]";

//  123-456-7890

sscanf 函数 (https:// /www.php.net/manual/en/function.sscanf.php) 将格式字符串作为第二个参数,告诉它如何解释第一个字符串中的字符。在这种情况下,它意味着2个字符(%2c)、3个字符、3个字符、4个字符。

通常,sscanf 函数还会包含其他参数变量来捕获提取的数据。如果没有,数据将返回到一个数组中,我将其称为 $parts

print 语句输出插值字符串。 $part[0] 被忽略。

我使用了类似的函数来格式化澳大利亚电话号码。

请注意,从存储电话号码的角度来看:

  • 电话号码是字符串,
  • 存储的数据不应包含格式,例如空格或连字符

Here’s my take:

$phone='+11234567890';
$parts=sscanf($phone,'%2c%3c%3c%4c');
print "$parts[1]-$parts[2]-$parts[3]";

//  123-456-7890

The sscanf function (https://www.php.net/manual/en/function.sscanf.php) takes as a second parameter a format string telling it how to interpret the characters from the first string. In this case, it means 2 characters (%2c), 3 characters, 3 characters, 4 characters.

Normally the sscanf function would also include additional parameter variables to capture the extracted data. If not, the data is return in an array which I have called $parts.

The print statement outputs the interpolated string. $part[0] is ignored.

I have used a similar function to format Australian phone numbers.

Note that from the perspective of storing the phone number:

  • phone numbers are strings
  • stored data should not include formatting, such as spaces or hyphens
不念旧人 2024-10-19 17:37:22

不要重新发明轮子!导入这个神奇的库:
https://github.com/giggsey/libphonenumber-for-php

$defaultCountry = 'SE'; // Based on the country of the user
$phoneUtil = PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse($phoneNumber, $defaultCountry);

return $phoneUtil->format($swissNumberProto, PhoneNumberFormat::INTERNATIONAL);

它基于Google 用于解析、格式化和验证国际电话号码的库:
https://github.com/google/libphonenumber

Don't reinvent the wheel! Import this amazing library:
https://github.com/giggsey/libphonenumber-for-php

$defaultCountry = 'SE'; // Based on the country of the user
$phoneUtil = PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse($phoneNumber, $defaultCountry);

return $phoneUtil->format($swissNumberProto, PhoneNumberFormat::INTERNATIONAL);

It is based on Google's library for parsing, formatting, and validating international phone numbers:
https://github.com/google/libphonenumber

清君侧 2024-10-19 17:37:22

这是一个简单的函数,用于以更欧洲(或瑞典?)的方式格式化 7 到 10 位数字的电话号码:

function formatPhone($num) {
    $num = preg_replace('/[^0-9]/', '', $num);
    $len = strlen($num);

    if($len == 7) $num = preg_replace('/([0-9]{2})([0-9]{2})([0-9]{3})/', '$1 $2 $3', $num);
    elseif($len == 8) $num = preg_replace('/([0-9]{3})([0-9]{2})([0-9]{3})/', '$1 - $2 $3', $num);
    elseif($len == 9) $num = preg_replace('/([0-9]{3})([0-9]{2})([0-9]{2})([0-9]{2})/', '$1 - $2 $3 $4', $num);
    elseif($len == 10) $num = preg_replace('/([0-9]{3})([0-9]{2})([0-9]{2})([0-9]{3})/', '$1 - $2 $3 $4', $num);

    return $num;
}

Here's a simple function for formatting phone numbers with 7 to 10 digits in a more European (or Swedish?) manner:

function formatPhone($num) {
    $num = preg_replace('/[^0-9]/', '', $num);
    $len = strlen($num);

    if($len == 7) $num = preg_replace('/([0-9]{2})([0-9]{2})([0-9]{3})/', '$1 $2 $3', $num);
    elseif($len == 8) $num = preg_replace('/([0-9]{3})([0-9]{2})([0-9]{3})/', '$1 - $2 $3', $num);
    elseif($len == 9) $num = preg_replace('/([0-9]{3})([0-9]{2})([0-9]{2})([0-9]{2})/', '$1 - $2 $3 $4', $num);
    elseif($len == 10) $num = preg_replace('/([0-9]{3})([0-9]{2})([0-9]{2})([0-9]{3})/', '$1 - $2 $3 $4', $num);

    return $num;
}
爱她像谁 2024-10-19 17:37:22

我认为使用一些正则表达式或一些 substr 调用(假设输入始终是该格式,并且不改变长度等)可以做到这一点,

类似的事情

$in = "+11234567890"; $output = substr($in,2,3)."-".substr($in,6,3)."-".substr($in,10,4);

应该做到这一点。

I see this being possible using either some regex, or a few substr calls (assuming the input is always of that format, and doesn't change length etc.)

something like

$in = "+11234567890"; $output = substr($in,2,3)."-".substr($in,6,3)."-".substr($in,10,4);

should do it.

A君 2024-10-19 17:37:22

我对一个非常相似(之前提出的)问题的建议(将 10 位数字字符串格式化为连字符的电话号码])需要很少的东西到这里就修改成功了。

早期的答案使用了 sscanf() 或 sprintf() ,但没有人愿意将这两个优秀的函数放在一起。

格式参数中基于占位符的语法为开发人员提供了高度的控制。对于大多数开发人员来说,定制/维护这种方法应该没有正则表达式那么令人畏惧。

vprintf() 允许您声明要使用单个值数组填充的占位符。

代码:(Demo)

$string = '+11234567890';

vprintf('%s-%s-%s', sscanf($string, '+1%3s%3s%4s'));
// 123-456-7890

或者返回值而不打印使用:

$variable = vsprintf('%s-%s-%s', sscanf($string, '+1%3s%3s%4s'));

或者因为只添加连字符,可以使用 implode 。

echo implode('-', sscanf($string, '+1%3s%3s%4s'));
// 123-456-7890

或者返回值而不打印使用:

$variable = implode('-', sscanf($string, '+1%3s%3s%4s'));

My advice on a VERY similar (earlier asked) question (Format 10-digit string into hyphenated phone number]) needs very little modification to be successful here.

There are earlier answers that use sscanf() or sprintf(), but no one bothered to put both excellent functions together.

The placeholder-based syntax in the format arguments offers a high degree of control to the developer. Customizing/Maintaining this approach should be less daunting than regex for most developers.

vprintf() allows you to declare placeholders to be populated using a single array of values.

Code: (Demo)

$string = '+11234567890';

vprintf('%s-%s-%s', sscanf($string, '+1%3s%3s%4s'));
// 123-456-7890

Or to return the value without printing use:

$variable = vsprintf('%s-%s-%s', sscanf($string, '+1%3s%3s%4s'));

Or because only adding hyphens, you can use implode.

echo implode('-', sscanf($string, '+1%3s%3s%4s'));
// 123-456-7890

Or to return the value without printing use:

$variable = implode('-', sscanf($string, '+1%3s%3s%4s'));
2024-10-19 17:37:22

这是针对没有国家代码

function format_phone_number($number) {
    $result = preg_replace('~.*(\d{2})[^\d]{0,7}(\d{4})[^\d]{0,7}(\d{4}).*~', '$1 $2 $3', $number);
    return $result;
}

结果的英国固定电话:

2012345678
becomes
20 1234 5678

This is for UK landlines without the Country Code

function format_phone_number($number) {
    $result = preg_replace('~.*(\d{2})[^\d]{0,7}(\d{4})[^\d]{0,7}(\d{4}).*~', '$1 $2 $3', $number);
    return $result;
}

Result:

2012345678
becomes
20 1234 5678
清风挽心 2024-10-19 17:37:22

尝试如下操作:

preg_replace('/\d{3}/', '$0-', str_replace('.', null, trim($number)), 2);

这将需要 $number 的 8881112222 并转换为 888-111-2222。希望这有帮助。

Try something like:

preg_replace('/\d{3}/', '$0-', str_replace('.', null, trim($number)), 2);

this would take a $number of 8881112222 and convert to 888-111-2222. Hope this helps.

风尘浪孓 2024-10-19 17:37:22

另一个选项 - 轻松更新以从配置接收格式。

$numbers = explode("\n", '(111) 222-3333
((111) 222-3333
1112223333
111 222-3333
111-222-3333
(111)2223333
+11234567890
    1-8002353551
    123-456-7890   -Hello!
+1 - 1234567890
');
foreach( $numbers AS $number ){
  echo comMember_format::phoneNumber($number) . '<br>';
}

// ************************************************************************
// Format Phone Number
public function phoneNumber( $number ){
  $txt = preg_replace('/[\s\-|\.|\(|\)]/','',$number);
  $format = '[$1?$1 :][$2?($2):x][$3: ]$4[$5: ]$6[$7? $7:]';
  if( preg_match('/^(.*)(\d{3})([^\d]*)(\d{3})([^\d]*)(\d{4})([^\d]{0,1}.*)$/', $txt, $matches) ){
    $result = $format;
    foreach( $matches AS $k => $v ){
      $str = preg_match('/\[\
.$k.'\?(.*?)\:(.*?)\]|\[\
.$k.'\:(.*?)\]|(\
.$k.'){1}/', $format, $filterMatch);
      if( $filterMatch ){
        $result = str_replace( $filterMatch[0], (!isset($filterMatch[3]) ? (strlen($v) ? str_replace( '
.$k, $v, $filterMatch[1] ) : $filterMatch[2]) : (strlen($v) ? $v : (isset($filterMatch[4]) ? '' : (isset($filterMatch[3]) ? $filterMatch[3] : '')))), $result );
      }
    }
    return $result;
  }
  return $number;
}

Another option - easily updated to receive a format from configuration.

$numbers = explode("\n", '(111) 222-3333
((111) 222-3333
1112223333
111 222-3333
111-222-3333
(111)2223333
+11234567890
    1-8002353551
    123-456-7890   -Hello!
+1 - 1234567890
');
foreach( $numbers AS $number ){
  echo comMember_format::phoneNumber($number) . '<br>';
}

// ************************************************************************
// Format Phone Number
public function phoneNumber( $number ){
  $txt = preg_replace('/[\s\-|\.|\(|\)]/','',$number);
  $format = '[$1?$1 :][$2?($2):x][$3: ]$4[$5: ]$6[$7? $7:]';
  if( preg_match('/^(.*)(\d{3})([^\d]*)(\d{3})([^\d]*)(\d{4})([^\d]{0,1}.*)$/', $txt, $matches) ){
    $result = $format;
    foreach( $matches AS $k => $v ){
      $str = preg_match('/\[\
.$k.'\?(.*?)\:(.*?)\]|\[\
.$k.'\:(.*?)\]|(\
.$k.'){1}/', $format, $filterMatch);
      if( $filterMatch ){
        $result = str_replace( $filterMatch[0], (!isset($filterMatch[3]) ? (strlen($v) ? str_replace( '
.$k, $v, $filterMatch[1] ) : $filterMatch[2]) : (strlen($v) ? $v : (isset($filterMatch[4]) ? '' : (isset($filterMatch[3]) ? $filterMatch[3] : '')))), $result );
      }
    }
    return $result;
  }
  return $number;
}
风吹雪碎 2024-10-19 17:37:22

这是接受电话号码和国家/地区代码的一个,例如
format电话号码('+254722456789','KE')

            function StripPhoneNumber($code, &$phoneNumber)
            {
                $code = strtoupper($code);
                $code = preg_replace('/[^A-Z]/','',$code);
                $countryCode = "";
                $countries = [];
                $countries[] = array("code" => "AF", "name" => "Afghanistan", "d_code" => "+93");
                $countries[] = array("code" => "AL", "name" => "Albania", "d_code" => "+355");
                $countries[] = array("code" => "DZ", "name" => "Algeria", "d_code" => "+213");
                $countries[] = array("code" => "AS", "name" => "American Samoa", "d_code" => "+1");
                $countries[] = array("code" => "AD", "name" => "Andorra", "d_code" => "+376");
                $countries[] = array("code" => "AO", "name" => "Angola", "d_code" => "+244");
                $countries[] = array("code" => "AI", "name" => "Anguilla", "d_code" => "+1");
                $countries[] = array("code" => "AG", "name" => "Antigua", "d_code" => "+1");
                $countries[] = array("code" => "AR", "name" => "Argentina", "d_code" => "+54");
                $countries[] = array("code" => "AM", "name" => "Armenia", "d_code" => "+374");
                $countries[] = array("code" => "AW", "name" => "Aruba", "d_code" => "+297");
                $countries[] = array("code" => "AU", "name" => "Australia", "d_code" => "+61");
                $countries[] = array("code" => "AT", "name" => "Austria", "d_code" => "+43");
                $countries[] = array("code" => "AZ", "name" => "Azerbaijan", "d_code" => "+994");
                $countries[] = array("code" => "BH", "name" => "Bahrain", "d_code" => "+973");
                $countries[] = array("code" => "BD", "name" => "Bangladesh", "d_code" => "+880");
                $countries[] = array("code" => "BB", "name" => "Barbados", "d_code" => "+1");
                $countries[] = array("code" => "BY", "name" => "Belarus", "d_code" => "+375");
                $countries[] = array("code" => "BE", "name" => "Belgium", "d_code" => "+32");
                $countries[] = array("code" => "BZ", "name" => "Belize", "d_code" => "+501");
                $countries[] = array("code" => "BJ", "name" => "Benin", "d_code" => "+229");
                $countries[] = array("code" => "BM", "name" => "Bermuda", "d_code" => "+1");
                $countries[] = array("code" => "BT", "name" => "Bhutan", "d_code" => "+975");
                $countries[] = array("code" => "BO", "name" => "Bolivia", "d_code" => "+591");
                $countries[] = array("code" => "BA", "name" => "Bosnia and Herzegovina", "d_code" => "+387");
                $countries[] = array("code" => "BW", "name" => "Botswana", "d_code" => "+267");
                $countries[] = array("code" => "BR", "name" => "Brazil", "d_code" => "+55");
                $countries[] = array("code" => "IO", "name" => "British Indian Ocean Territory", "d_code" => "+246");
                $countries[] = array("code" => "VG", "name" => "British Virgin Islands", "d_code" => "+1");
                $countries[] = array("code" => "BN", "name" => "Brunei", "d_code" => "+673");
                $countries[] = array("code" => "BG", "name" => "Bulgaria", "d_code" => "+359");
                $countries[] = array("code" => "BF", "name" => "Burkina Faso", "d_code" => "+226");
                $countries[] = array("code" => "MM", "name" => "Burma Myanmar", "d_code" => "+95");
                $countries[] = array("code" => "BI", "name" => "Burundi", "d_code" => "+257");
                $countries[] = array("code" => "KH", "name" => "Cambodia", "d_code" => "+855");
                $countries[] = array("code" => "CM", "name" => "Cameroon", "d_code" => "+237");
                $countries[] = array("code" => "CA", "name" => "Canada", "d_code" => "+1");
                $countries[] = array("code" => "CV", "name" => "Cape Verde", "d_code" => "+238");
                $countries[] = array("code" => "KY", "name" => "Cayman Islands", "d_code" => "+1");
                $countries[] = array("code" => "CF", "name" => "Central African Republic", "d_code" => "+236");
                $countries[] = array("code" => "TD", "name" => "Chad", "d_code" => "+235");
                $countries[] = array("code" => "CL", "name" => "Chile", "d_code" => "+56");
                $countries[] = array("code" => "CN", "name" => "China", "d_code" => "+86");
                $countries[] = array("code" => "CO", "name" => "Colombia", "d_code" => "+57");
                $countries[] = array("code" => "KM", "name" => "Comoros", "d_code" => "+269");
                $countries[] = array("code" => "CK", "name" => "Cook Islands", "d_code" => "+682");
                $countries[] = array("code" => "CR", "name" => "Costa Rica", "d_code" => "+506");
                $countries[] = array("code" => "CI", "name" => "Côte d'Ivoire", "d_code" => "+225");
                $countries[] = array("code" => "HR", "name" => "Croatia", "d_code" => "+385");
                $countries[] = array("code" => "CU", "name" => "Cuba", "d_code" => "+53");
                $countries[] = array("code" => "CY", "name" => "Cyprus", "d_code" => "+357");
                $countries[] = array("code" => "CZ", "name" => "Czech Republic", "d_code" => "+420");
                $countries[] = array("code" => "CD", "name" => "Democratic Republic of Congo", "d_code" => "+243");
                $countries[] = array("code" => "DK", "name" => "Denmark", "d_code" => "+45");
                $countries[] = array("code" => "DJ", "name" => "Djibouti", "d_code" => "+253");
                $countries[] = array("code" => "DM", "name" => "Dominica", "d_code" => "+1");
                $countries[] = array("code" => "DO", "name" => "Dominican Republic", "d_code" => "+1");
                $countries[] = array("code" => "EC", "name" => "Ecuador", "d_code" => "+593");
                $countries[] = array("code" => "EG", "name" => "Egypt", "d_code" => "+20");
                $countries[] = array("code" => "SV", "name" => "El Salvador", "d_code" => "+503");
                $countries[] = array("code" => "GQ", "name" => "Equatorial Guinea", "d_code" => "+240");
                $countries[] = array("code" => "ER", "name" => "Eritrea", "d_code" => "+291");
                $countries[] = array("code" => "EE", "name" => "Estonia", "d_code" => "+372");
                $countries[] = array("code" => "ET", "name" => "Ethiopia", "d_code" => "+251");
                $countries[] = array("code" => "FK", "name" => "Falkland Islands", "d_code" => "+500");
                $countries[] = array("code" => "FO", "name" => "Faroe Islands", "d_code" => "+298");
                $countries[] = array("code" => "FM", "name" => "Federated States of Micronesia", "d_code" => "+691");
                $countries[] = array("code" => "FJ", "name" => "Fiji", "d_code" => "+679");
                $countries[] = array("code" => "FI", "name" => "Finland", "d_code" => "+358");
                $countries[] = array("code" => "FR", "name" => "France", "d_code" => "+33");
                $countries[] = array("code" => "GF", "name" => "French Guiana", "d_code" => "+594");
                $countries[] = array("code" => "PF", "name" => "French Polynesia", "d_code" => "+689");
                $countries[] = array("code" => "GA", "name" => "Gabon", "d_code" => "+241");
                $countries[] = array("code" => "GE", "name" => "Georgia", "d_code" => "+995");
                $countries[] = array("code" => "DE", "name" => "Germany", "d_code" => "+49");
                $countries[] = array("code" => "GH", "name" => "Ghana", "d_code" => "+233");
                $countries[] = array("code" => "GI", "name" => "Gibraltar", "d_code" => "+350");
                $countries[] = array("code" => "GR", "name" => "Greece", "d_code" => "+30");
                $countries[] = array("code" => "GL", "name" => "Greenland", "d_code" => "+299");
                $countries[] = array("code" => "GD", "name" => "Grenada", "d_code" => "+1");
                $countries[] = array("code" => "GP", "name" => "Guadeloupe", "d_code" => "+590");
                $countries[] = array("code" => "GU", "name" => "Guam", "d_code" => "+1");
                $countries[] = array("code" => "GT", "name" => "Guatemala", "d_code" => "+502");
                $countries[] = array("code" => "GN", "name" => "Guinea", "d_code" => "+224");
                $countries[] = array("code" => "GW", "name" => "Guinea-Bissau", "d_code" => "+245");
                $countries[] = array("code" => "GY", "name" => "Guyana", "d_code" => "+592");
                $countries[] = array("code" => "HT", "name" => "Haiti", "d_code" => "+509");
                $countries[] = array("code" => "HN", "name" => "Honduras", "d_code" => "+504");
                $countries[] = array("code" => "HK", "name" => "Hong Kong", "d_code" => "+852");
                $countries[] = array("code" => "HU", "name" => "Hungary", "d_code" => "+36");
                $countries[] = array("code" => "IS", "name" => "Iceland", "d_code" => "+354");
                $countries[] = array("code" => "IN", "name" => "India", "d_code" => "+91");
                $countries[] = array("code" => "ID", "name" => "Indonesia", "d_code" => "+62");
                $countries[] = array("code" => "IR", "name" => "Iran", "d_code" => "+98");
                $countries[] = array("code" => "IQ", "name" => "Iraq", "d_code" => "+964");
                $countries[] = array("code" => "IE", "name" => "Ireland", "d_code" => "+353");
                $countries[] = array("code" => "IL", "name" => "Israel", "d_code" => "+972");
                $countries[] = array("code" => "IT", "name" => "Italy", "d_code" => "+39");
                $countries[] = array("code" => "JM", "name" => "Jamaica", "d_code" => "+1");
                $countries[] = array("code" => "JP", "name" => "Japan", "d_code" => "+81");
                $countries[] = array("code" => "JO", "name" => "Jordan", "d_code" => "+962");
                $countries[] = array("code" => "KZ", "name" => "Kazakhstan", "d_code" => "+7");
                $countries[] = array("code" => "KE", "name" => "Kenya", "d_code" => "+254");
                $countries[] = array("code" => "KI", "name" => "Kiribati", "d_code" => "+686");
                $countries[] = array("code" => "XK", "name" => "Kosovo", "d_code" => "+381");
                $countries[] = array("code" => "KW", "name" => "Kuwait", "d_code" => "+965");
                $countries[] = array("code" => "KG", "name" => "Kyrgyzstan", "d_code" => "+996");
                $countries[] = array("code" => "LA", "name" => "Laos", "d_code" => "+856");
                $countries[] = array("code" => "LV", "name" => "Latvia", "d_code" => "+371");
                $countries[] = array("code" => "LB", "name" => "Lebanon", "d_code" => "+961");
                $countries[] = array("code" => "LS", "name" => "Lesotho", "d_code" => "+266");
                $countries[] = array("code" => "LR", "name" => "Liberia", "d_code" => "+231");
                $countries[] = array("code" => "LY", "name" => "Libya", "d_code" => "+218");
                $countries[] = array("code" => "LI", "name" => "Liechtenstein", "d_code" => "+423");
                $countries[] = array("code" => "LT", "name" => "Lithuania", "d_code" => "+370");
                $countries[] = array("code" => "LU", "name" => "Luxembourg", "d_code" => "+352");
                $countries[] = array("code" => "MO", "name" => "Macau", "d_code" => "+853");
                $countries[] = array("code" => "MK", "name" => "Macedonia", "d_code" => "+389");
                $countries[] = array("code" => "MG", "name" => "Madagascar", "d_code" => "+261");
                $countries[] = array("code" => "MW", "name" => "Malawi", "d_code" => "+265");
                $countries[] = array("code" => "MY", "name" => "Malaysia", "d_code" => "+60");
                $countries[] = array("code" => "MV", "name" => "Maldives", "d_code" => "+960");
                $countries[] = array("code" => "ML", "name" => "Mali", "d_code" => "+223");
                $countries[] = array("code" => "MT", "name" => "Malta", "d_code" => "+356");
                $countries[] = array("code" => "MH", "name" => "Marshall Islands", "d_code" => "+692");
                $countries[] = array("code" => "MQ", "name" => "Martinique", "d_code" => "+596");
                $countries[] = array("code" => "MR", "name" => "Mauritania", "d_code" => "+222");
                $countries[] = array("code" => "MU", "name" => "Mauritius", "d_code" => "+230");
                $countries[] = array("code" => "YT", "name" => "Mayotte", "d_code" => "+262");
                $countries[] = array("code" => "MX", "name" => "Mexico", "d_code" => "+52");
                $countries[] = array("code" => "MD", "name" => "Moldova", "d_code" => "+373");
                $countries[] = array("code" => "MC", "name" => "Monaco", "d_code" => "+377");
                $countries[] = array("code" => "MN", "name" => "Mongolia", "d_code" => "+976");
                $countries[] = array("code" => "ME", "name" => "Montenegro", "d_code" => "+382");
                $countries[] = array("code" => "MS", "name" => "Montserrat", "d_code" => "+1");
                $countries[] = array("code" => "MA", "name" => "Morocco", "d_code" => "+212");
                $countries[] = array("code" => "MZ", "name" => "Mozambique", "d_code" => "+258");
                $countries[] = array("code" => "NA", "name" => "Namibia", "d_code" => "+264");
                $countries[] = array("code" => "NR", "name" => "Nauru", "d_code" => "+674");
                $countries[] = array("code" => "NP", "name" => "Nepal", "d_code" => "+977");
                $countries[] = array("code" => "NL", "name" => "Netherlands", "d_code" => "+31");
                $countries[] = array("code" => "AN", "name" => "Netherlands Antilles", "d_code" => "+599");
                $countries[] = array("code" => "NC", "name" => "New Caledonia", "d_code" => "+687");
                $countries[] = array("code" => "NZ", "name" => "New Zealand", "d_code" => "+64");
                $countries[] = array("code" => "NI", "name" => "Nicaragua", "d_code" => "+505");
                $countries[] = array("code" => "NE", "name" => "Niger", "d_code" => "+227");
                $countries[] = array("code" => "NG", "name" => "Nigeria", "d_code" => "+234");
                $countries[] = array("code" => "NU", "name" => "Niue", "d_code" => "+683");
                $countries[] = array("code" => "NF", "name" => "Norfolk Island", "d_code" => "+672");
                $countries[] = array("code" => "KP", "name" => "North Korea", "d_code" => "+850");
                $countries[] = array("code" => "MP", "name" => "Northern Mariana Islands", "d_code" => "+1");
                $countries[] = array("code" => "NO", "name" => "Norway", "d_code" => "+47");
                $countries[] = array("code" => "OM", "name" => "Oman", "d_code" => "+968");
                $countries[] = array("code" => "PK", "name" => "Pakistan", "d_code" => "+92");
                $countries[] = array("code" => "PW", "name" => "Palau", "d_code" => "+680");
                $countries[] = array("code" => "PS", "name" => "Palestine", "d_code" => "+970");
                $countries[] = array("code" => "PA", "name" => "Panama", "d_code" => "+507");
                $countries[] = array("code" => "PG", "name" => "Papua New Guinea", "d_code" => "+675");
                $countries[] = array("code" => "PY", "name" => "Paraguay", "d_code" => "+595");
                $countries[] = array("code" => "PE", "name" => "Peru", "d_code" => "+51");
                $countries[] = array("code" => "PH", "name" => "Philippines", "d_code" => "+63");
                $countries[] = array("code" => "PL", "name" => "Poland", "d_code" => "+48");
                $countries[] = array("code" => "PT", "name" => "Portugal", "d_code" => "+351");
                $countries[] = array("code" => "PR", "name" => "Puerto Rico", "d_code" => "+1");
                $countries[] = array("code" => "QA", "name" => "Qatar", "d_code" => "+974");
                $countries[] = array("code" => "CG", "name" => "Republic of the Congo", "d_code" => "+242");
                $countries[] = array("code" => "RE", "name" => "Réunion", "d_code" => "+262");
                $countries[] = array("code" => "RO", "name" => "Romania", "d_code" => "+40");
                $countries[] = array("code" => "RU", "name" => "Russia", "d_code" => "+7");
                $countries[] = array("code" => "RW", "name" => "Rwanda", "d_code" => "+250");
                $countries[] = array("code" => "BL", "name" => "Saint Barthélemy", "d_code" => "+590");
                $countries[] = array("code" => "SH", "name" => "Saint Helena", "d_code" => "+290");
                $countries[] = array("code" => "KN", "name" => "Saint Kitts and Nevis", "d_code" => "+1");
                $countries[] = array("code" => "MF", "name" => "Saint Martin", "d_code" => "+590");
                $countries[] = array("code" => "PM", "name" => "Saint Pierre and Miquelon", "d_code" => "+508");
                $countries[] = array("code" => "VC", "name" => "Saint Vincent and the Grenadines", "d_code" => "+1");
                $countries[] = array("code" => "WS", "name" => "Samoa", "d_code" => "+685");
                $countries[] = array("code" => "SM", "name" => "San Marino", "d_code" => "+378");
                $countries[] = array("code" => "ST", "name" => "São Tomé and Príncipe", "d_code" => "+239");
                $countries[] = array("code" => "SA", "name" => "Saudi Arabia", "d_code" => "+966");
                $countries[] = array("code" => "SN", "name" => "Senegal", "d_code" => "+221");
                $countries[] = array("code" => "RS", "name" => "Serbia", "d_code" => "+381");
                $countries[] = array("code" => "SC", "name" => "Seychelles", "d_code" => "+248");
                $countries[] = array("code" => "SL", "name" => "Sierra Leone", "d_code" => "+232");
                $countries[] = array("code" => "SG", "name" => "Singapore", "d_code" => "+65");
                $countries[] = array("code" => "SK", "name" => "Slovakia", "d_code" => "+421");
                $countries[] = array("code" => "SI", "name" => "Slovenia", "d_code" => "+386");
                $countries[] = array("code" => "SB", "name" => "Solomon Islands", "d_code" => "+677");
                $countries[] = array("code" => "SO", "name" => "Somalia", "d_code" => "+252");
                $countries[] = array("code" => "ZA", "name" => "South Africa", "d_code" => "+27");
                $countries[] = array("code" => "KR", "name" => "South Korea", "d_code" => "+82");
                $countries[] = array("code" => "ES", "name" => "Spain", "d_code" => "+34");
                $countries[] = array("code" => "LK", "name" => "Sri Lanka", "d_code" => "+94");
                $countries[] = array("code" => "LC", "name" => "St. Lucia", "d_code" => "+1");
                $countries[] = array("code" => "SD", "name" => "Sudan", "d_code" => "+249");
                $countries[] = array("code" => "SR", "name" => "Suriname", "d_code" => "+597");
                $countries[] = array("code" => "SZ", "name" => "Swaziland", "d_code" => "+268");
                $countries[] = array("code" => "SE", "name" => "Sweden", "d_code" => "+46");
                $countries[] = array("code" => "CH", "name" => "Switzerland", "d_code" => "+41");
                $countries[] = array("code" => "SY", "name" => "Syria", "d_code" => "+963");
                $countries[] = array("code" => "TW", "name" => "Taiwan", "d_code" => "+886");
                $countries[] = array("code" => "TJ", "name" => "Tajikistan", "d_code" => "+992");
                $countries[] = array("code" => "TZ", "name" => "Tanzania", "d_code" => "+255");
                $countries[] = array("code" => "TH", "name" => "Thailand", "d_code" => "+66");
                $countries[] = array("code" => "BS", "name" => "The Bahamas", "d_code" => "+1");
                $countries[] = array("code" => "GM", "name" => "The Gambia", "d_code" => "+220");
                $countries[] = array("code" => "TL", "name" => "Timor-Leste", "d_code" => "+670");
                $countries[] = array("code" => "TG", "name" => "Togo", "d_code" => "+228");
                $countries[] = array("code" => "TK", "name" => "Tokelau", "d_code" => "+690");
                $countries[] = array("code" => "TO", "name" => "Tonga", "d_code" => "+676");
                $countries[] = array("code" => "TT", "name" => "Trinidad and Tobago", "d_code" => "+1");
                $countries[] = array("code" => "TN", "name" => "Tunisia", "d_code" => "+216");
                $countries[] = array("code" => "TR", "name" => "Turkey", "d_code" => "+90");
                $countries[] = array("code" => "TM", "name" => "Turkmenistan", "d_code" => "+993");
                $countries[] = array("code" => "TC", "name" => "Turks and Caicos Islands", "d_code" => "+1");
                $countries[] = array("code" => "TV", "name" => "Tuvalu", "d_code" => "+688");
                $countries[] = array("code" => "UG", "name" => "Uganda", "d_code" => "+256");
                $countries[] = array("code" => "UA", "name" => "Ukraine", "d_code" => "+380");
                $countries[] = array("code" => "AE", "name" => "United Arab Emirates", "d_code" => "+971");
                $countries[] = array("code" => "GB", "name" => "United Kingdom", "d_code" => "+44");
                $countries[] = array("code" => "US", "name" => "United States", "d_code" => "+1");
                $countries[] = array("code" => "UY", "name" => "Uruguay", "d_code" => "+598");
                $countries[] = array("code" => "VI", "name" => "US Virgin Islands", "d_code" => "+1");
                $countries[] = array("code" => "UZ", "name" => "Uzbekistan", "d_code" => "+998");
                $countries[] = array("code" => "VU", "name" => "Vanuatu", "d_code" => "+678");
                $countries[] = array("code" => "VA", "name" => "Vatican City", "d_code" => "+39");
                $countries[] = array("code" => "VE", "name" => "Venezuela", "d_code" => "+58");
                $countries[] = array("code" => "VN", "name" => "Vietnam", "d_code" => "+84");
                $countries[] = array("code" => "WF", "name" => "Wallis and Futuna", "d_code" => "+681");
                $countries[] = array("code" => "YE", "name" => "Yemen", "d_code" => "+967");
                $countries[] = array("code" => "ZM", "name" => "Zambia", "d_code" => "+260");
                $countries[] = array("code" => "ZW", "name" => "Zimbabwe", "d_code" => "+263");

                $code = strtoupper($code);
                $UsephoneNumber = " "."+";
                $UsephoneNumber = $UsephoneNumber.(preg_replace('/[^0-9]/','',$phoneNumber));

                for($index=0; $index<count($countries); $index++)
                {
                    $array1 = $countries[$index];
                    if($array1['code'] == $code)
                    {
                        $dCode = $array1['d_code'];
                        $explode_ = explode($dCode, $UsephoneNumber, 2);
                        if(count($explode_) == 2)
                        {
                            $phoneNumber = $explode_[1];
                            $countryCode = $dCode;
                        }
                        break;
                    }
                }

                return $countryCode;
            }


            function formatPhoneNumber($phoneNumber, $code="") {

                $countryCode = StripPhoneNumber($code, $phoneNumber);
                $phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);

                if(strlen($phoneNumber) > 10) {
                    $countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
                    $areaCode = substr($phoneNumber, -10, 3);
                    $nextThree = substr($phoneNumber, -7, 3);
                    $lastFour = substr($phoneNumber, -4, 4);

                    $phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
                }
                else if(strlen($phoneNumber) == 10) {
                    $areaCode = substr($phoneNumber, 0, 3);
                    $nextThree = substr($phoneNumber, 3, 3);
                    $lastFour = substr($phoneNumber, 6, 4);

                    $phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
                }
                else if(strlen($phoneNumber) == 7) {
                    $nextThree = substr($phoneNumber, 0, 3);
                    $lastFour = substr($phoneNumber, 3, 4);

                    $phoneNumber = $nextThree.'-'.$lastFour;
                }
                else
                {
                    $sub = "";
                    while(strlen($phoneNumber) >= 3)
                    {
                        if( strlen($phoneNumber) >= 3 && (strlen($phoneNumber) % 2 != 0) )
                        {
                            $sub = $sub.substr($phoneNumber, 0, 3)." ";
                            $phoneNumber = substr($phoneNumber, 3);
                        }
                        else
                        if( (strlen($phoneNumber) % 2 == 0) && strlen($phoneNumber) >= 2 )
                        {
                           $sub = $sub.substr($phoneNumber, 0, 2)." ";
                           $phoneNumber = substr($phoneNumber, 2);
                        }
                    }

                    if( strlen($phoneNumber) > 0)
                    {
                        $sub = $sub.$phoneNumber." ";
                    }

                    $phoneNumber = trim($sub," ");

                }

                return $countryCode." ".$phoneNumber;
            }

Here is one that accepts a phonenumber as well as a country code, like
formatPhoneNumber('+254722456789','KE')

            function StripPhoneNumber($code, &$phoneNumber)
            {
                $code = strtoupper($code);
                $code = preg_replace('/[^A-Z]/','',$code);
                $countryCode = "";
                $countries = [];
                $countries[] = array("code" => "AF", "name" => "Afghanistan", "d_code" => "+93");
                $countries[] = array("code" => "AL", "name" => "Albania", "d_code" => "+355");
                $countries[] = array("code" => "DZ", "name" => "Algeria", "d_code" => "+213");
                $countries[] = array("code" => "AS", "name" => "American Samoa", "d_code" => "+1");
                $countries[] = array("code" => "AD", "name" => "Andorra", "d_code" => "+376");
                $countries[] = array("code" => "AO", "name" => "Angola", "d_code" => "+244");
                $countries[] = array("code" => "AI", "name" => "Anguilla", "d_code" => "+1");
                $countries[] = array("code" => "AG", "name" => "Antigua", "d_code" => "+1");
                $countries[] = array("code" => "AR", "name" => "Argentina", "d_code" => "+54");
                $countries[] = array("code" => "AM", "name" => "Armenia", "d_code" => "+374");
                $countries[] = array("code" => "AW", "name" => "Aruba", "d_code" => "+297");
                $countries[] = array("code" => "AU", "name" => "Australia", "d_code" => "+61");
                $countries[] = array("code" => "AT", "name" => "Austria", "d_code" => "+43");
                $countries[] = array("code" => "AZ", "name" => "Azerbaijan", "d_code" => "+994");
                $countries[] = array("code" => "BH", "name" => "Bahrain", "d_code" => "+973");
                $countries[] = array("code" => "BD", "name" => "Bangladesh", "d_code" => "+880");
                $countries[] = array("code" => "BB", "name" => "Barbados", "d_code" => "+1");
                $countries[] = array("code" => "BY", "name" => "Belarus", "d_code" => "+375");
                $countries[] = array("code" => "BE", "name" => "Belgium", "d_code" => "+32");
                $countries[] = array("code" => "BZ", "name" => "Belize", "d_code" => "+501");
                $countries[] = array("code" => "BJ", "name" => "Benin", "d_code" => "+229");
                $countries[] = array("code" => "BM", "name" => "Bermuda", "d_code" => "+1");
                $countries[] = array("code" => "BT", "name" => "Bhutan", "d_code" => "+975");
                $countries[] = array("code" => "BO", "name" => "Bolivia", "d_code" => "+591");
                $countries[] = array("code" => "BA", "name" => "Bosnia and Herzegovina", "d_code" => "+387");
                $countries[] = array("code" => "BW", "name" => "Botswana", "d_code" => "+267");
                $countries[] = array("code" => "BR", "name" => "Brazil", "d_code" => "+55");
                $countries[] = array("code" => "IO", "name" => "British Indian Ocean Territory", "d_code" => "+246");
                $countries[] = array("code" => "VG", "name" => "British Virgin Islands", "d_code" => "+1");
                $countries[] = array("code" => "BN", "name" => "Brunei", "d_code" => "+673");
                $countries[] = array("code" => "BG", "name" => "Bulgaria", "d_code" => "+359");
                $countries[] = array("code" => "BF", "name" => "Burkina Faso", "d_code" => "+226");
                $countries[] = array("code" => "MM", "name" => "Burma Myanmar", "d_code" => "+95");
                $countries[] = array("code" => "BI", "name" => "Burundi", "d_code" => "+257");
                $countries[] = array("code" => "KH", "name" => "Cambodia", "d_code" => "+855");
                $countries[] = array("code" => "CM", "name" => "Cameroon", "d_code" => "+237");
                $countries[] = array("code" => "CA", "name" => "Canada", "d_code" => "+1");
                $countries[] = array("code" => "CV", "name" => "Cape Verde", "d_code" => "+238");
                $countries[] = array("code" => "KY", "name" => "Cayman Islands", "d_code" => "+1");
                $countries[] = array("code" => "CF", "name" => "Central African Republic", "d_code" => "+236");
                $countries[] = array("code" => "TD", "name" => "Chad", "d_code" => "+235");
                $countries[] = array("code" => "CL", "name" => "Chile", "d_code" => "+56");
                $countries[] = array("code" => "CN", "name" => "China", "d_code" => "+86");
                $countries[] = array("code" => "CO", "name" => "Colombia", "d_code" => "+57");
                $countries[] = array("code" => "KM", "name" => "Comoros", "d_code" => "+269");
                $countries[] = array("code" => "CK", "name" => "Cook Islands", "d_code" => "+682");
                $countries[] = array("code" => "CR", "name" => "Costa Rica", "d_code" => "+506");
                $countries[] = array("code" => "CI", "name" => "Côte d'Ivoire", "d_code" => "+225");
                $countries[] = array("code" => "HR", "name" => "Croatia", "d_code" => "+385");
                $countries[] = array("code" => "CU", "name" => "Cuba", "d_code" => "+53");
                $countries[] = array("code" => "CY", "name" => "Cyprus", "d_code" => "+357");
                $countries[] = array("code" => "CZ", "name" => "Czech Republic", "d_code" => "+420");
                $countries[] = array("code" => "CD", "name" => "Democratic Republic of Congo", "d_code" => "+243");
                $countries[] = array("code" => "DK", "name" => "Denmark", "d_code" => "+45");
                $countries[] = array("code" => "DJ", "name" => "Djibouti", "d_code" => "+253");
                $countries[] = array("code" => "DM", "name" => "Dominica", "d_code" => "+1");
                $countries[] = array("code" => "DO", "name" => "Dominican Republic", "d_code" => "+1");
                $countries[] = array("code" => "EC", "name" => "Ecuador", "d_code" => "+593");
                $countries[] = array("code" => "EG", "name" => "Egypt", "d_code" => "+20");
                $countries[] = array("code" => "SV", "name" => "El Salvador", "d_code" => "+503");
                $countries[] = array("code" => "GQ", "name" => "Equatorial Guinea", "d_code" => "+240");
                $countries[] = array("code" => "ER", "name" => "Eritrea", "d_code" => "+291");
                $countries[] = array("code" => "EE", "name" => "Estonia", "d_code" => "+372");
                $countries[] = array("code" => "ET", "name" => "Ethiopia", "d_code" => "+251");
                $countries[] = array("code" => "FK", "name" => "Falkland Islands", "d_code" => "+500");
                $countries[] = array("code" => "FO", "name" => "Faroe Islands", "d_code" => "+298");
                $countries[] = array("code" => "FM", "name" => "Federated States of Micronesia", "d_code" => "+691");
                $countries[] = array("code" => "FJ", "name" => "Fiji", "d_code" => "+679");
                $countries[] = array("code" => "FI", "name" => "Finland", "d_code" => "+358");
                $countries[] = array("code" => "FR", "name" => "France", "d_code" => "+33");
                $countries[] = array("code" => "GF", "name" => "French Guiana", "d_code" => "+594");
                $countries[] = array("code" => "PF", "name" => "French Polynesia", "d_code" => "+689");
                $countries[] = array("code" => "GA", "name" => "Gabon", "d_code" => "+241");
                $countries[] = array("code" => "GE", "name" => "Georgia", "d_code" => "+995");
                $countries[] = array("code" => "DE", "name" => "Germany", "d_code" => "+49");
                $countries[] = array("code" => "GH", "name" => "Ghana", "d_code" => "+233");
                $countries[] = array("code" => "GI", "name" => "Gibraltar", "d_code" => "+350");
                $countries[] = array("code" => "GR", "name" => "Greece", "d_code" => "+30");
                $countries[] = array("code" => "GL", "name" => "Greenland", "d_code" => "+299");
                $countries[] = array("code" => "GD", "name" => "Grenada", "d_code" => "+1");
                $countries[] = array("code" => "GP", "name" => "Guadeloupe", "d_code" => "+590");
                $countries[] = array("code" => "GU", "name" => "Guam", "d_code" => "+1");
                $countries[] = array("code" => "GT", "name" => "Guatemala", "d_code" => "+502");
                $countries[] = array("code" => "GN", "name" => "Guinea", "d_code" => "+224");
                $countries[] = array("code" => "GW", "name" => "Guinea-Bissau", "d_code" => "+245");
                $countries[] = array("code" => "GY", "name" => "Guyana", "d_code" => "+592");
                $countries[] = array("code" => "HT", "name" => "Haiti", "d_code" => "+509");
                $countries[] = array("code" => "HN", "name" => "Honduras", "d_code" => "+504");
                $countries[] = array("code" => "HK", "name" => "Hong Kong", "d_code" => "+852");
                $countries[] = array("code" => "HU", "name" => "Hungary", "d_code" => "+36");
                $countries[] = array("code" => "IS", "name" => "Iceland", "d_code" => "+354");
                $countries[] = array("code" => "IN", "name" => "India", "d_code" => "+91");
                $countries[] = array("code" => "ID", "name" => "Indonesia", "d_code" => "+62");
                $countries[] = array("code" => "IR", "name" => "Iran", "d_code" => "+98");
                $countries[] = array("code" => "IQ", "name" => "Iraq", "d_code" => "+964");
                $countries[] = array("code" => "IE", "name" => "Ireland", "d_code" => "+353");
                $countries[] = array("code" => "IL", "name" => "Israel", "d_code" => "+972");
                $countries[] = array("code" => "IT", "name" => "Italy", "d_code" => "+39");
                $countries[] = array("code" => "JM", "name" => "Jamaica", "d_code" => "+1");
                $countries[] = array("code" => "JP", "name" => "Japan", "d_code" => "+81");
                $countries[] = array("code" => "JO", "name" => "Jordan", "d_code" => "+962");
                $countries[] = array("code" => "KZ", "name" => "Kazakhstan", "d_code" => "+7");
                $countries[] = array("code" => "KE", "name" => "Kenya", "d_code" => "+254");
                $countries[] = array("code" => "KI", "name" => "Kiribati", "d_code" => "+686");
                $countries[] = array("code" => "XK", "name" => "Kosovo", "d_code" => "+381");
                $countries[] = array("code" => "KW", "name" => "Kuwait", "d_code" => "+965");
                $countries[] = array("code" => "KG", "name" => "Kyrgyzstan", "d_code" => "+996");
                $countries[] = array("code" => "LA", "name" => "Laos", "d_code" => "+856");
                $countries[] = array("code" => "LV", "name" => "Latvia", "d_code" => "+371");
                $countries[] = array("code" => "LB", "name" => "Lebanon", "d_code" => "+961");
                $countries[] = array("code" => "LS", "name" => "Lesotho", "d_code" => "+266");
                $countries[] = array("code" => "LR", "name" => "Liberia", "d_code" => "+231");
                $countries[] = array("code" => "LY", "name" => "Libya", "d_code" => "+218");
                $countries[] = array("code" => "LI", "name" => "Liechtenstein", "d_code" => "+423");
                $countries[] = array("code" => "LT", "name" => "Lithuania", "d_code" => "+370");
                $countries[] = array("code" => "LU", "name" => "Luxembourg", "d_code" => "+352");
                $countries[] = array("code" => "MO", "name" => "Macau", "d_code" => "+853");
                $countries[] = array("code" => "MK", "name" => "Macedonia", "d_code" => "+389");
                $countries[] = array("code" => "MG", "name" => "Madagascar", "d_code" => "+261");
                $countries[] = array("code" => "MW", "name" => "Malawi", "d_code" => "+265");
                $countries[] = array("code" => "MY", "name" => "Malaysia", "d_code" => "+60");
                $countries[] = array("code" => "MV", "name" => "Maldives", "d_code" => "+960");
                $countries[] = array("code" => "ML", "name" => "Mali", "d_code" => "+223");
                $countries[] = array("code" => "MT", "name" => "Malta", "d_code" => "+356");
                $countries[] = array("code" => "MH", "name" => "Marshall Islands", "d_code" => "+692");
                $countries[] = array("code" => "MQ", "name" => "Martinique", "d_code" => "+596");
                $countries[] = array("code" => "MR", "name" => "Mauritania", "d_code" => "+222");
                $countries[] = array("code" => "MU", "name" => "Mauritius", "d_code" => "+230");
                $countries[] = array("code" => "YT", "name" => "Mayotte", "d_code" => "+262");
                $countries[] = array("code" => "MX", "name" => "Mexico", "d_code" => "+52");
                $countries[] = array("code" => "MD", "name" => "Moldova", "d_code" => "+373");
                $countries[] = array("code" => "MC", "name" => "Monaco", "d_code" => "+377");
                $countries[] = array("code" => "MN", "name" => "Mongolia", "d_code" => "+976");
                $countries[] = array("code" => "ME", "name" => "Montenegro", "d_code" => "+382");
                $countries[] = array("code" => "MS", "name" => "Montserrat", "d_code" => "+1");
                $countries[] = array("code" => "MA", "name" => "Morocco", "d_code" => "+212");
                $countries[] = array("code" => "MZ", "name" => "Mozambique", "d_code" => "+258");
                $countries[] = array("code" => "NA", "name" => "Namibia", "d_code" => "+264");
                $countries[] = array("code" => "NR", "name" => "Nauru", "d_code" => "+674");
                $countries[] = array("code" => "NP", "name" => "Nepal", "d_code" => "+977");
                $countries[] = array("code" => "NL", "name" => "Netherlands", "d_code" => "+31");
                $countries[] = array("code" => "AN", "name" => "Netherlands Antilles", "d_code" => "+599");
                $countries[] = array("code" => "NC", "name" => "New Caledonia", "d_code" => "+687");
                $countries[] = array("code" => "NZ", "name" => "New Zealand", "d_code" => "+64");
                $countries[] = array("code" => "NI", "name" => "Nicaragua", "d_code" => "+505");
                $countries[] = array("code" => "NE", "name" => "Niger", "d_code" => "+227");
                $countries[] = array("code" => "NG", "name" => "Nigeria", "d_code" => "+234");
                $countries[] = array("code" => "NU", "name" => "Niue", "d_code" => "+683");
                $countries[] = array("code" => "NF", "name" => "Norfolk Island", "d_code" => "+672");
                $countries[] = array("code" => "KP", "name" => "North Korea", "d_code" => "+850");
                $countries[] = array("code" => "MP", "name" => "Northern Mariana Islands", "d_code" => "+1");
                $countries[] = array("code" => "NO", "name" => "Norway", "d_code" => "+47");
                $countries[] = array("code" => "OM", "name" => "Oman", "d_code" => "+968");
                $countries[] = array("code" => "PK", "name" => "Pakistan", "d_code" => "+92");
                $countries[] = array("code" => "PW", "name" => "Palau", "d_code" => "+680");
                $countries[] = array("code" => "PS", "name" => "Palestine", "d_code" => "+970");
                $countries[] = array("code" => "PA", "name" => "Panama", "d_code" => "+507");
                $countries[] = array("code" => "PG", "name" => "Papua New Guinea", "d_code" => "+675");
                $countries[] = array("code" => "PY", "name" => "Paraguay", "d_code" => "+595");
                $countries[] = array("code" => "PE", "name" => "Peru", "d_code" => "+51");
                $countries[] = array("code" => "PH", "name" => "Philippines", "d_code" => "+63");
                $countries[] = array("code" => "PL", "name" => "Poland", "d_code" => "+48");
                $countries[] = array("code" => "PT", "name" => "Portugal", "d_code" => "+351");
                $countries[] = array("code" => "PR", "name" => "Puerto Rico", "d_code" => "+1");
                $countries[] = array("code" => "QA", "name" => "Qatar", "d_code" => "+974");
                $countries[] = array("code" => "CG", "name" => "Republic of the Congo", "d_code" => "+242");
                $countries[] = array("code" => "RE", "name" => "Réunion", "d_code" => "+262");
                $countries[] = array("code" => "RO", "name" => "Romania", "d_code" => "+40");
                $countries[] = array("code" => "RU", "name" => "Russia", "d_code" => "+7");
                $countries[] = array("code" => "RW", "name" => "Rwanda", "d_code" => "+250");
                $countries[] = array("code" => "BL", "name" => "Saint Barthélemy", "d_code" => "+590");
                $countries[] = array("code" => "SH", "name" => "Saint Helena", "d_code" => "+290");
                $countries[] = array("code" => "KN", "name" => "Saint Kitts and Nevis", "d_code" => "+1");
                $countries[] = array("code" => "MF", "name" => "Saint Martin", "d_code" => "+590");
                $countries[] = array("code" => "PM", "name" => "Saint Pierre and Miquelon", "d_code" => "+508");
                $countries[] = array("code" => "VC", "name" => "Saint Vincent and the Grenadines", "d_code" => "+1");
                $countries[] = array("code" => "WS", "name" => "Samoa", "d_code" => "+685");
                $countries[] = array("code" => "SM", "name" => "San Marino", "d_code" => "+378");
                $countries[] = array("code" => "ST", "name" => "São Tomé and Príncipe", "d_code" => "+239");
                $countries[] = array("code" => "SA", "name" => "Saudi Arabia", "d_code" => "+966");
                $countries[] = array("code" => "SN", "name" => "Senegal", "d_code" => "+221");
                $countries[] = array("code" => "RS", "name" => "Serbia", "d_code" => "+381");
                $countries[] = array("code" => "SC", "name" => "Seychelles", "d_code" => "+248");
                $countries[] = array("code" => "SL", "name" => "Sierra Leone", "d_code" => "+232");
                $countries[] = array("code" => "SG", "name" => "Singapore", "d_code" => "+65");
                $countries[] = array("code" => "SK", "name" => "Slovakia", "d_code" => "+421");
                $countries[] = array("code" => "SI", "name" => "Slovenia", "d_code" => "+386");
                $countries[] = array("code" => "SB", "name" => "Solomon Islands", "d_code" => "+677");
                $countries[] = array("code" => "SO", "name" => "Somalia", "d_code" => "+252");
                $countries[] = array("code" => "ZA", "name" => "South Africa", "d_code" => "+27");
                $countries[] = array("code" => "KR", "name" => "South Korea", "d_code" => "+82");
                $countries[] = array("code" => "ES", "name" => "Spain", "d_code" => "+34");
                $countries[] = array("code" => "LK", "name" => "Sri Lanka", "d_code" => "+94");
                $countries[] = array("code" => "LC", "name" => "St. Lucia", "d_code" => "+1");
                $countries[] = array("code" => "SD", "name" => "Sudan", "d_code" => "+249");
                $countries[] = array("code" => "SR", "name" => "Suriname", "d_code" => "+597");
                $countries[] = array("code" => "SZ", "name" => "Swaziland", "d_code" => "+268");
                $countries[] = array("code" => "SE", "name" => "Sweden", "d_code" => "+46");
                $countries[] = array("code" => "CH", "name" => "Switzerland", "d_code" => "+41");
                $countries[] = array("code" => "SY", "name" => "Syria", "d_code" => "+963");
                $countries[] = array("code" => "TW", "name" => "Taiwan", "d_code" => "+886");
                $countries[] = array("code" => "TJ", "name" => "Tajikistan", "d_code" => "+992");
                $countries[] = array("code" => "TZ", "name" => "Tanzania", "d_code" => "+255");
                $countries[] = array("code" => "TH", "name" => "Thailand", "d_code" => "+66");
                $countries[] = array("code" => "BS", "name" => "The Bahamas", "d_code" => "+1");
                $countries[] = array("code" => "GM", "name" => "The Gambia", "d_code" => "+220");
                $countries[] = array("code" => "TL", "name" => "Timor-Leste", "d_code" => "+670");
                $countries[] = array("code" => "TG", "name" => "Togo", "d_code" => "+228");
                $countries[] = array("code" => "TK", "name" => "Tokelau", "d_code" => "+690");
                $countries[] = array("code" => "TO", "name" => "Tonga", "d_code" => "+676");
                $countries[] = array("code" => "TT", "name" => "Trinidad and Tobago", "d_code" => "+1");
                $countries[] = array("code" => "TN", "name" => "Tunisia", "d_code" => "+216");
                $countries[] = array("code" => "TR", "name" => "Turkey", "d_code" => "+90");
                $countries[] = array("code" => "TM", "name" => "Turkmenistan", "d_code" => "+993");
                $countries[] = array("code" => "TC", "name" => "Turks and Caicos Islands", "d_code" => "+1");
                $countries[] = array("code" => "TV", "name" => "Tuvalu", "d_code" => "+688");
                $countries[] = array("code" => "UG", "name" => "Uganda", "d_code" => "+256");
                $countries[] = array("code" => "UA", "name" => "Ukraine", "d_code" => "+380");
                $countries[] = array("code" => "AE", "name" => "United Arab Emirates", "d_code" => "+971");
                $countries[] = array("code" => "GB", "name" => "United Kingdom", "d_code" => "+44");
                $countries[] = array("code" => "US", "name" => "United States", "d_code" => "+1");
                $countries[] = array("code" => "UY", "name" => "Uruguay", "d_code" => "+598");
                $countries[] = array("code" => "VI", "name" => "US Virgin Islands", "d_code" => "+1");
                $countries[] = array("code" => "UZ", "name" => "Uzbekistan", "d_code" => "+998");
                $countries[] = array("code" => "VU", "name" => "Vanuatu", "d_code" => "+678");
                $countries[] = array("code" => "VA", "name" => "Vatican City", "d_code" => "+39");
                $countries[] = array("code" => "VE", "name" => "Venezuela", "d_code" => "+58");
                $countries[] = array("code" => "VN", "name" => "Vietnam", "d_code" => "+84");
                $countries[] = array("code" => "WF", "name" => "Wallis and Futuna", "d_code" => "+681");
                $countries[] = array("code" => "YE", "name" => "Yemen", "d_code" => "+967");
                $countries[] = array("code" => "ZM", "name" => "Zambia", "d_code" => "+260");
                $countries[] = array("code" => "ZW", "name" => "Zimbabwe", "d_code" => "+263");

                $code = strtoupper($code);
                $UsephoneNumber = " "."+";
                $UsephoneNumber = $UsephoneNumber.(preg_replace('/[^0-9]/','',$phoneNumber));

                for($index=0; $index<count($countries); $index++)
                {
                    $array1 = $countries[$index];
                    if($array1['code'] == $code)
                    {
                        $dCode = $array1['d_code'];
                        $explode_ = explode($dCode, $UsephoneNumber, 2);
                        if(count($explode_) == 2)
                        {
                            $phoneNumber = $explode_[1];
                            $countryCode = $dCode;
                        }
                        break;
                    }
                }

                return $countryCode;
            }


            function formatPhoneNumber($phoneNumber, $code="") {

                $countryCode = StripPhoneNumber($code, $phoneNumber);
                $phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);

                if(strlen($phoneNumber) > 10) {
                    $countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
                    $areaCode = substr($phoneNumber, -10, 3);
                    $nextThree = substr($phoneNumber, -7, 3);
                    $lastFour = substr($phoneNumber, -4, 4);

                    $phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
                }
                else if(strlen($phoneNumber) == 10) {
                    $areaCode = substr($phoneNumber, 0, 3);
                    $nextThree = substr($phoneNumber, 3, 3);
                    $lastFour = substr($phoneNumber, 6, 4);

                    $phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
                }
                else if(strlen($phoneNumber) == 7) {
                    $nextThree = substr($phoneNumber, 0, 3);
                    $lastFour = substr($phoneNumber, 3, 4);

                    $phoneNumber = $nextThree.'-'.$lastFour;
                }
                else
                {
                    $sub = "";
                    while(strlen($phoneNumber) >= 3)
                    {
                        if( strlen($phoneNumber) >= 3 && (strlen($phoneNumber) % 2 != 0) )
                        {
                            $sub = $sub.substr($phoneNumber, 0, 3)." ";
                            $phoneNumber = substr($phoneNumber, 3);
                        }
                        else
                        if( (strlen($phoneNumber) % 2 == 0) && strlen($phoneNumber) >= 2 )
                        {
                           $sub = $sub.substr($phoneNumber, 0, 2)." ";
                           $phoneNumber = substr($phoneNumber, 2);
                        }
                    }

                    if( strlen($phoneNumber) > 0)
                    {
                        $sub = $sub.$phoneNumber." ";
                    }

                    $phoneNumber = trim($sub," ");

                }

                return $countryCode." ".$phoneNumber;
            }
只有一腔孤勇 2024-10-19 17:37:22

所有,

我想我已经解决了。适用于当前输入文件并具有以下 2 个函数来完成此任务!

function format_phone_number:

        function format_phone_number ( $mynum, $mask ) {
        /*********************************************************************/
        /*   Purpose: Return either masked phone number or false             */
        /*     Masks: Val=1 or xxx xxx xxxx                                             */
        /*            Val=2 or xxx xxx.xxxx                                             */
        /*            Val=3 or xxx.xxx.xxxx                                             */
        /*            Val=4 or (xxx) xxx xxxx                                           */
        /*            Val=5 or (xxx) xxx.xxxx                                           */
        /*            Val=6 or (xxx).xxx.xxxx                                           */
        /*            Val=7 or (xxx) xxx-xxxx                                           */
        /*            Val=8 or (xxx)-xxx-xxxx                                           */
        /*********************************************************************/         
        $val_num        = self::validate_phone_number ( $mynum );
        if ( !$val_num && !is_string ( $mynum ) ) { 
            echo "Number $mynum is not a valid phone number! \n";
            return false;
        }   // end if !$val_num
        if ( ( $mask == 1 ) || ( $mask == 'xxx xxx xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '$1 $2 $3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 1
        if ( ( $mask == 2 ) || ( $mask == 'xxx xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '$1 $2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 2
        if ( ( $mask == 3 ) || ( $mask == 'xxx.xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '$1.$2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 3
        if ( ( $mask == 4 ) || ( $mask == '(xxx) xxx xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1) $2 $3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 4
        if ( ( $mask == 5 ) || ( $mask == '(xxx) xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1) $2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 5
        if ( ( $mask == 6 ) || ( $mask == '(xxx).xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1).$2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 6
        if ( ( $mask == 7 ) || ( $mask == '(xxx) xxx-xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1) $2-$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 7
        if ( ( $mask == 8 ) || ( $mask == '(xxx)-xxx-xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1)-$2-$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 8
        return false;       // Returns false if no conditions meet or input
    }  // end function format_phone_number

function validate_phone_number:

        function validate_phone_number ( $phone ) {
        /*********************************************************************/
        /*   Purpose:   To determine if the passed string is a valid phone  */
        /*              number following one of the establish formatting        */
        /*                  styles for phone numbers.  This function also breaks    */
        /*                  a valid number into it's respective components of:      */
        /*                          3-digit area code,                                      */
        /*                          3-digit exchange code,                                  */
        /*                          4-digit subscriber number                               */
        /*                  and validates the number against 10 digit US NANPA  */
        /*                  guidelines.                                                         */
        /*********************************************************************/         
        $format_pattern =   '/^(?:(?:\((?=\d{3}\)))?(\d{3})(?:(?<=\(\d{3})\))'.
                                    '?[\s.\/-]?)?(\d{3})[\s\.\/-]?(\d{4})\s?(?:(?:(?:'.
                                    '(?:e|x|ex|ext)\.?\:?|extension\:?)\s?)(?=\d+)'.
                                    '(\d+))?$/';
        $nanpa_pattern      =   '/^(?:1)?(?(?!(37|96))[2-9][0-8][0-9](?<!(11)))?'.
                                    '[2-9][0-9]{2}(?<!(11))[0-9]{4}(?<!(555(01([0-9]'.
                                    '[0-9])|1212)))$/';

        // Init array of variables to false
        $valid = array('format' =>  false,
                            'nanpa' => false,
                            'ext'       => false,
                            'all'       => false);

        //Check data against the format analyzer
        if ( preg_match ( $format_pattern, $phone, $matchset ) ) {
            $valid['format'] = true;    
        }

        //If formatted properly, continue
        //if($valid['format']) {
        if ( !$valid['format'] ) {
            return false;
        } else {
            //Set array of new components
            $components =   array ( 'ac' => $matchset[1], //area code
                                                            'xc' => $matchset[2], //exchange code
                                                            'sn' => $matchset[3] //subscriber number
                                                            );
            //              $components =   array ( 'ac' => $matchset[1], //area code
            //                                              'xc' => $matchset[2], //exchange code
            //                                              'sn' => $matchset[3], //subscriber number
            //                                              'xn' => $matchset[4] //extension number             
            //                                              );

            //Set array of number variants
            $numbers    =   array ( 'original' => $matchset[0],
                                        'stripped' => substr(preg_replace('[\D]', '', $matchset[0]), 0, 10)
                                        );

            //Now let's check the first ten digits against NANPA standards
            if(preg_match($nanpa_pattern, $numbers['stripped'])) {
                $valid['nanpa'] = true;
            }

            //If the NANPA guidelines have been met, continue
            if ( $valid['nanpa'] ) {
                if ( !empty ( $components['xn'] ) ) {
                    if ( preg_match ( '/^[\d]{1,6}$/', $components['xn'] ) ) {
                        $valid['ext'] = true;
                    }   // end if if preg_match 
                } else {
                    $valid['ext'] = true;
                }   // end if if  !empty
            }   // end if $valid nanpa

            //If the extension number is valid or non-existent, continue
            if ( $valid['ext'] ) {
                $valid['all'] = true;
            }   // end if $valid ext
        }   // end if $valid
        return $valid['all'];
    }   // end functon validate_phone_number

注意,我在类库中有这个,因此第一个函数/方法调用了“self::validate_phone_number”。

请注意“validate_phone_number”函数的第 32 行,我在其中添加了:

            if ( !$valid['format'] ) {
            return false;
        } else {

如果电话号码无效,则需要返回错误的值。

仍然需要针对更多数据进行测试,但是使用当前格式处理当前数据,并且我对这个特定的数据批次使用样式“8”。

此外,我还注释掉了“扩展”逻辑,因为我不断从中收到错误,因为我的数据中没有任何该信息。

All,

I think I fixed it. Working for current input files and have following 2 functions to get this done!

function format_phone_number:

        function format_phone_number ( $mynum, $mask ) {
        /*********************************************************************/
        /*   Purpose: Return either masked phone number or false             */
        /*     Masks: Val=1 or xxx xxx xxxx                                             */
        /*            Val=2 or xxx xxx.xxxx                                             */
        /*            Val=3 or xxx.xxx.xxxx                                             */
        /*            Val=4 or (xxx) xxx xxxx                                           */
        /*            Val=5 or (xxx) xxx.xxxx                                           */
        /*            Val=6 or (xxx).xxx.xxxx                                           */
        /*            Val=7 or (xxx) xxx-xxxx                                           */
        /*            Val=8 or (xxx)-xxx-xxxx                                           */
        /*********************************************************************/         
        $val_num        = self::validate_phone_number ( $mynum );
        if ( !$val_num && !is_string ( $mynum ) ) { 
            echo "Number $mynum is not a valid phone number! \n";
            return false;
        }   // end if !$val_num
        if ( ( $mask == 1 ) || ( $mask == 'xxx xxx xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '$1 $2 $3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 1
        if ( ( $mask == 2 ) || ( $mask == 'xxx xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '$1 $2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 2
        if ( ( $mask == 3 ) || ( $mask == 'xxx.xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '$1.$2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 3
        if ( ( $mask == 4 ) || ( $mask == '(xxx) xxx xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1) $2 $3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 4
        if ( ( $mask == 5 ) || ( $mask == '(xxx) xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1) $2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 5
        if ( ( $mask == 6 ) || ( $mask == '(xxx).xxx.xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1).$2.$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 6
        if ( ( $mask == 7 ) || ( $mask == '(xxx) xxx-xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1) $2-$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 7
        if ( ( $mask == 8 ) || ( $mask == '(xxx)-xxx-xxxx' ) ) { 
            $phone = preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', 
                    '($1)-$2-$3'." \n", $mynum);
            return $phone;
        }   // end if $mask == 8
        return false;       // Returns false if no conditions meet or input
    }  // end function format_phone_number

function validate_phone_number:

        function validate_phone_number ( $phone ) {
        /*********************************************************************/
        /*   Purpose:   To determine if the passed string is a valid phone  */
        /*              number following one of the establish formatting        */
        /*                  styles for phone numbers.  This function also breaks    */
        /*                  a valid number into it's respective components of:      */
        /*                          3-digit area code,                                      */
        /*                          3-digit exchange code,                                  */
        /*                          4-digit subscriber number                               */
        /*                  and validates the number against 10 digit US NANPA  */
        /*                  guidelines.                                                         */
        /*********************************************************************/         
        $format_pattern =   '/^(?:(?:\((?=\d{3}\)))?(\d{3})(?:(?<=\(\d{3})\))'.
                                    '?[\s.\/-]?)?(\d{3})[\s\.\/-]?(\d{4})\s?(?:(?:(?:'.
                                    '(?:e|x|ex|ext)\.?\:?|extension\:?)\s?)(?=\d+)'.
                                    '(\d+))?$/';
        $nanpa_pattern      =   '/^(?:1)?(?(?!(37|96))[2-9][0-8][0-9](?<!(11)))?'.
                                    '[2-9][0-9]{2}(?<!(11))[0-9]{4}(?<!(555(01([0-9]'.
                                    '[0-9])|1212)))$/';

        // Init array of variables to false
        $valid = array('format' =>  false,
                            'nanpa' => false,
                            'ext'       => false,
                            'all'       => false);

        //Check data against the format analyzer
        if ( preg_match ( $format_pattern, $phone, $matchset ) ) {
            $valid['format'] = true;    
        }

        //If formatted properly, continue
        //if($valid['format']) {
        if ( !$valid['format'] ) {
            return false;
        } else {
            //Set array of new components
            $components =   array ( 'ac' => $matchset[1], //area code
                                                            'xc' => $matchset[2], //exchange code
                                                            'sn' => $matchset[3] //subscriber number
                                                            );
            //              $components =   array ( 'ac' => $matchset[1], //area code
            //                                              'xc' => $matchset[2], //exchange code
            //                                              'sn' => $matchset[3], //subscriber number
            //                                              'xn' => $matchset[4] //extension number             
            //                                              );

            //Set array of number variants
            $numbers    =   array ( 'original' => $matchset[0],
                                        'stripped' => substr(preg_replace('[\D]', '', $matchset[0]), 0, 10)
                                        );

            //Now let's check the first ten digits against NANPA standards
            if(preg_match($nanpa_pattern, $numbers['stripped'])) {
                $valid['nanpa'] = true;
            }

            //If the NANPA guidelines have been met, continue
            if ( $valid['nanpa'] ) {
                if ( !empty ( $components['xn'] ) ) {
                    if ( preg_match ( '/^[\d]{1,6}$/', $components['xn'] ) ) {
                        $valid['ext'] = true;
                    }   // end if if preg_match 
                } else {
                    $valid['ext'] = true;
                }   // end if if  !empty
            }   // end if $valid nanpa

            //If the extension number is valid or non-existent, continue
            if ( $valid['ext'] ) {
                $valid['all'] = true;
            }   // end if $valid ext
        }   // end if $valid
        return $valid['all'];
    }   // end functon validate_phone_number

Notice I have this in a class lib, so thus the "self::validate_phone_number" call from the first function/method.

Notice line # 32 of the "validate_phone_number" function where I added the:

            if ( !$valid['format'] ) {
            return false;
        } else {

to get me the false return needed if not valid phone number.

Still need to test this against more data, but working on current data, with current format and I'm using style '8' for this particular data batch.

Also I commented out the "extension" logic as I was constantly getting errors from it, seeing I do not have any of that info in my data.

暖阳 2024-10-19 17:37:22

这需要 7、10 和 11 位数字,删除附加字符并通过从右向左浏览字符串来添加破折号。将破折号更改为空格或点。

$raw_phone = preg_replace('/\D/', '', $raw_phone);
$temp = str_split($raw_phone);
$phone_number = "";
for ($x=count($temp)-1;$x>=0;$x--) {
    if ($x === count($temp) - 5 || $x === count($temp) - 8 || $x === count($temp) - 11) {
        $phone_number = "-" . $phone_number;
    }
    $phone_number = $temp[$x] . $phone_number;
}
echo $phone_number;

this takes 7, 10 and 11 digit, removes additional characters and adds dashes by going right to left through the string. change the dash to a space or dot.

$raw_phone = preg_replace('/\D/', '', $raw_phone);
$temp = str_split($raw_phone);
$phone_number = "";
for ($x=count($temp)-1;$x>=0;$x--) {
    if ($x === count($temp) - 5 || $x === count($temp) - 8 || $x === count($temp) - 11) {
        $phone_number = "-" . $phone_number;
    }
    $phone_number = $temp[$x] . $phone_number;
}
echo $phone_number;
一个人的旅程 2024-10-19 17:37:22

我知道OP正在请求123-456-7890格式,但是,基于 John Dul的回答,我修改为返回括号格式的电话号码,例如(123)456-7890。此号码仅处理 7 和 10 位数字。

function format_phone_string( $raw_number ) {

    // remove everything but numbers
    $raw_number = preg_replace( '/\D/', '', $raw_number );

    // split each number into an array
    $arr_number = str_split($raw_number);

    // add a dummy value to the beginning of the array
    array_unshift( $arr_number, 'dummy' );

    // remove the dummy value so now the array keys start at 1
    unset($arr_number[0]);

    // get the number of numbers in the number
    $num_number = count($arr_number);

    // loop through each number backward starting at the end
    for ( $x = $num_number; $x >= 0; $x-- ) {

        if ( $x === $num_number - 4 ) {
            // before the fourth to last number

            $phone_number = "-" . $phone_number;
        }
        else if ( $x === $num_number - 7 && $num_number > 7 ) {
            // before the seventh to last number
            // and only if the number is more than 7 digits

            $phone_number = ") " . $phone_number;
        }
        else if ( $x === $num_number - 10 ) {
            // before the tenth to last number

            $phone_number = "(" . $phone_number;
        }

        // concatenate each number (possibly with modifications) back on
        $phone_number = $arr_number[$x] . $phone_number;
    }

    return $phone_number;
}

I know the OP is requesting a 123-456-7890 format, but, based on John Dul's answer, I modified it to return the phone number in parentheses format, e.g. (123) 456-7890. This one only handles 7 and 10 digit numbers.

function format_phone_string( $raw_number ) {

    // remove everything but numbers
    $raw_number = preg_replace( '/\D/', '', $raw_number );

    // split each number into an array
    $arr_number = str_split($raw_number);

    // add a dummy value to the beginning of the array
    array_unshift( $arr_number, 'dummy' );

    // remove the dummy value so now the array keys start at 1
    unset($arr_number[0]);

    // get the number of numbers in the number
    $num_number = count($arr_number);

    // loop through each number backward starting at the end
    for ( $x = $num_number; $x >= 0; $x-- ) {

        if ( $x === $num_number - 4 ) {
            // before the fourth to last number

            $phone_number = "-" . $phone_number;
        }
        else if ( $x === $num_number - 7 && $num_number > 7 ) {
            // before the seventh to last number
            // and only if the number is more than 7 digits

            $phone_number = ") " . $phone_number;
        }
        else if ( $x === $num_number - 10 ) {
            // before the tenth to last number

            $phone_number = "(" . $phone_number;
        }

        // concatenate each number (possibly with modifications) back on
        $phone_number = $arr_number[$x] . $phone_number;
    }

    return $phone_number;
}
可是我不能没有你 2024-10-19 17:37:22

请查看基于 substr 的可以更改格式的函数

function phone(string $in): string
{
    $FORMAT_PHONE = [1,3,3,4];
    $result =[];
    $position = 0;
    foreach ($FORMAT_PHONE as $key => $item){
        $result[] = substr($in, $position, $item);
        $position += $item;
    }
    return '+'.implode('-',$result);
}

Please have a look at substr based function that can change formats

function phone(string $in): string
{
    $FORMAT_PHONE = [1,3,3,4];
    $result =[];
    $position = 0;
    foreach ($FORMAT_PHONE as $key => $item){
        $result[] = substr($in, $position, $item);
        $position += $item;
    }
    return '+'.implode('-',$result);
}
不及他 2024-10-19 17:37:22

英国电话格式

对于我开发的应用程序,我发现人们从人类可读的形式“正确”输入了电话号码,但插入了各种随机字符,例如“-”“/”“+44”等。问题在于它需要与之对话的云应用程序对于格式非常具体。我没有使用正则表达式(可能会让用户感到沮丧),而是创建了一个对象类,该对象类在持久性模块处理之前将输入的数字处理为正确的格式。

输出的格式确保任何接收软件将输出解释为文本而不是整数(然后会立即丢失前导零),并且格式与英国电信关于号码的准则一致格式化——通过将长数字分成易于记忆的小组,也有助于人类记忆。

+441234567890 生产 (01234) 567 890

02012345678 生产 (020) 1234 5678

1923123456 产生 (01923) 123 456

01923123456 产生 (01923) 123 456

01923hello 这是文本 123456 生成 (01923) 123 456

括号中的号码交换部分的重要性在于,在英国和大多数其他国家/地区,可以在同一交换机中的号码之间进行呼叫,而无需进行交换部分。 但这不适用于 07、08 和 09 系列电话号码。

我确信有更有效的解决方案,但事实证明这个解决方案非常可靠。通过在末尾添加 teleNum 函数,可以轻松容纳更多格式。

该过程是从调用脚本中调用的,因此

$telephone = New Telephone;
$formattedPhoneNumber = $telephone->toIntegerForm($num)

`

<?php
class   Telephone 
{   
    public function toIntegerForm($num) {
        /*
         * This section takes the number, whatever its format, and purifies it to just digits without any space or other characters
         * This ensures that the formatter only has one type of input to deal with
         */
        $number = str_replace('+44', '0', $num);
        $length = strlen($number);
        $digits = '';
        $i=0;
        while ($i<$length){
            $digits .= $this->first( substr($number,$i,1) , $i);
            $i++;
        }
        if (strlen($number)<10) {return '';}
        return $this->toTextForm($digits);
    }
    public function toTextForm($number) {

        /*
         * This works on the purified number to then format it according to the group code
         * Numbers starting 01 and 07 are grouped 5 3 3
         * Other numbers are grouped 3 4 4 
         * 
         */
        if (substr($number,0,1) == '+') { return $number; }
        $group = substr($number,0,2);
        switch ($group){
            case "02" :
                $formattedNumber = $this->teleNum($number, 3, 4); // If number commences '02N' then output will be (02N) NNNN NNNN
                break;
            default :
                $formattedNumber = $this->teleNum($number, 5, 3); // Otherwise the ooutput will be (0NNNN) NNN NNN
        }
        return $formattedNumber;

    }
    private function first($digit,$position){
        if ($digit == '+' && $position == 0) {return $digit;};
        if (!is_numeric($digit)){
            return '';
        }
        if ($position == 0) {
            return ($digit == '0' ) ? $digit :  '0'.$digit;
        } else {
            return $digit;
        } 
    }
    private function teleNum($number,$a,$b){
        /*
         * Formats the required output 
         */
        $c=strlen($number)-($a+$b);
        $bit1 = substr($number,0,$a);
        $bit2 = substr($number,$a,$b);
        $bit3 = substr($number,$a+$b,$c);
        return '('.$bit1.') '.$bit2." ".$bit3;
    }
}

United Kingdom Phone Formats

For the application I developed, I found that people entered their phone number 'correctly' from a human readable form, but inserted varous random characters such as '-' '/' '+44' etc. The problem was that the cloud app that it needed to talk to was quite specific about the format. Rather than use a regular expression (can be frustrating for the user) I created an object class which processes the entered number into the correct format before being processed by the persistence module.

The format of the output ensures that any receiving software interprets the output as text rather than an integer (which would then immediately lose the leading zero) and the format is consistent with British Telecoms guidelines on number formatting - which also aids human memorability by dividing a long number into small, easily memorised, groups.

+441234567890 produces (01234) 567 890

02012345678 produces (020) 1234 5678

1923123456 produces (01923) 123 456

01923123456 produces (01923) 123 456

01923hello this is text123456 produces (01923) 123 456

The significance of the exchange segment of the number - in parentheses - is that in the UK, and most other countries, calls between numbers in the same exchange can be made omitting the exchange segment. This does not apply to 07, 08 and 09 series phone numbers however.

I'm sure that there are more efficient solutions, but this one has proved extremely reliable. More formats can easily be accomodated by adding to the teleNum function at the end.

The procedure is invoked from the calling script thus

$telephone = New Telephone;
$formattedPhoneNumber = $telephone->toIntegerForm($num)

`

<?php
class   Telephone 
{   
    public function toIntegerForm($num) {
        /*
         * This section takes the number, whatever its format, and purifies it to just digits without any space or other characters
         * This ensures that the formatter only has one type of input to deal with
         */
        $number = str_replace('+44', '0', $num);
        $length = strlen($number);
        $digits = '';
        $i=0;
        while ($i<$length){
            $digits .= $this->first( substr($number,$i,1) , $i);
            $i++;
        }
        if (strlen($number)<10) {return '';}
        return $this->toTextForm($digits);
    }
    public function toTextForm($number) {

        /*
         * This works on the purified number to then format it according to the group code
         * Numbers starting 01 and 07 are grouped 5 3 3
         * Other numbers are grouped 3 4 4 
         * 
         */
        if (substr($number,0,1) == '+') { return $number; }
        $group = substr($number,0,2);
        switch ($group){
            case "02" :
                $formattedNumber = $this->teleNum($number, 3, 4); // If number commences '02N' then output will be (02N) NNNN NNNN
                break;
            default :
                $formattedNumber = $this->teleNum($number, 5, 3); // Otherwise the ooutput will be (0NNNN) NNN NNN
        }
        return $formattedNumber;

    }
    private function first($digit,$position){
        if ($digit == '+' && $position == 0) {return $digit;};
        if (!is_numeric($digit)){
            return '';
        }
        if ($position == 0) {
            return ($digit == '0' ) ? $digit :  '0'.$digit;
        } else {
            return $digit;
        } 
    }
    private function teleNum($number,$a,$b){
        /*
         * Formats the required output 
         */
        $c=strlen($number)-($a+$b);
        $bit1 = substr($number,0,$a);
        $bit2 = substr($number,$a,$b);
        $bit3 = substr($number,$a+$b,$c);
        return '('.$bit1.') '.$bit2." ".$bit3;
    }
}
赠佳期 2024-10-19 17:37:22

这是我在很多代码中使用的一个小函数。我知道这个问题已经很老了,但我在寻找其他东西时偶然发现了它,所以我想这可能会帮助其他人,idk...

/**
 * Format a provided phone number, with options
 * 
 * @param  string  $phone        The phone number to validate
 * @param  string  $phoneFormat  The format we want the number to be in, standard 10 digit no CC by default
 * @param  string  $countryCode  The country code, default is 1 for US
 * @return string                The formatted phone number, or 'INVALID' if the number could not be parsed 
 *                                 to the given format, or 'EMPTY' if this lead has no phone
 */
function validatePhone(string $phone, string $phoneFormat = "<NPA><NXX><XXXX>", string $countryCode = '1'): string
{
    if (empty($phone)) {
        return "EMPTY";
    }

    $phone = preg_replace("/(^\+1)|(^\+)|(^1)/", "", $phone);
    if (preg_match("/^.*(?P<NPA>\d{3}).*(?P<NXX>\d{3}).*(?P<XXXX>\d{4})$/", $phone, $parts)) {
        $phoneFormat = preg_replace('/<CC>/', $countryCode, $phoneFormat);
        $phoneFormat = preg_replace('/<NPA>/', $parts['NPA'], $phoneFormat);
        $phoneFormat = preg_replace('/<NXX>/', $parts['NXX'], $phoneFormat);
        $phoneFormat = preg_replace('/<XXXX>/', $parts['XXXX'], $phoneFormat);

        return $phoneFormat;
    } else {
        return "INVALID";
    }
}

This is a little function I use for a lot of my code. I know this SO question is pretty old, but I stumbled on it while looking for something else so figured maybe this would help someone else, idk...

/**
 * Format a provided phone number, with options
 * 
 * @param  string  $phone        The phone number to validate
 * @param  string  $phoneFormat  The format we want the number to be in, standard 10 digit no CC by default
 * @param  string  $countryCode  The country code, default is 1 for US
 * @return string                The formatted phone number, or 'INVALID' if the number could not be parsed 
 *                                 to the given format, or 'EMPTY' if this lead has no phone
 */
function validatePhone(string $phone, string $phoneFormat = "<NPA><NXX><XXXX>", string $countryCode = '1'): string
{
    if (empty($phone)) {
        return "EMPTY";
    }

    $phone = preg_replace("/(^\+1)|(^\+)|(^1)/", "", $phone);
    if (preg_match("/^.*(?P<NPA>\d{3}).*(?P<NXX>\d{3}).*(?P<XXXX>\d{4})$/", $phone, $parts)) {
        $phoneFormat = preg_replace('/<CC>/', $countryCode, $phoneFormat);
        $phoneFormat = preg_replace('/<NPA>/', $parts['NPA'], $phoneFormat);
        $phoneFormat = preg_replace('/<NXX>/', $parts['NXX'], $phoneFormat);
        $phoneFormat = preg_replace('/<XXXX>/', $parts['XXXX'], $phoneFormat);

        return $phoneFormat;
    } else {
        return "INVALID";
    }
}
旧伤还要旧人安 2024-10-19 17:37:22

我继续重新发明轮子。该函数将处理国家代码、区号和各种格式的扩展名:

    /**
     * Returns the given phone number in a standardized format.
     * Accepts formatted phone number strings, include area codes, country codes, and extensions.
     *
     * @param string $phone_number
     * @return string
     */
    function format_phone_number(string $phone_number) {
        if (!preg_match('/^(?:\+?(?<countrycode>\d{1,2}))?[- ]?(?:[(\[]?(?<areacode>\d{3})[)\]]?)?[- ]?(?:(?<first3>\d{3})[- ]?(?<last4>\d{4}))[- ]?(?:(?:[:;,]|e\w+| )+[ \t]*(?<ext>\d+))?$/', $phone_number, $parts))
            return preg_replace('/[^0-9]/', '', $phone_number);
        
        $phone = [
            "country_code" => !empty($parts["countrycode"]) ? "+{$parts["countrycode"]}" : null,
            "area_code"    => !empty($parts["areacode"   ]) ? "({$parts["areacode"]})"   : null,
            "body"         => "{$parts["first3"]}-{$parts["last4"]}",
            "extension"    => !empty($parts["ext"        ]) ? "ext. {$parts["ext"]}"     : null,
        ];
    
        return implode(" ", array_filter($phone));
    }

示例:

    format_phone_number("1231231234 ext 5"    ); // (123) 123-1234 ext. 5
    format_phone_number("123 123 1234:4"      ); // (123) 123-1234 ext. 4
    format_phone_number("123-1231234; 345"    ); // (123) 123-1234 ext. 345
    format_phone_number("123-123-1234"        ); // (123) 123-1234
    format_phone_number("1231231234"          ); // (123) 123-1234
    format_phone_number("1234567"             ); // 123-4567
    format_phone_number("123456789012 43"     ); // +12 (345) 678-9012 ext. 43
    format_phone_number("(123) 456-7890 ext 4"); // (123) 456-7890 ext. 4

以下是正则表达式的细分:

    $countrycode = "(?:\+?(?<countrycode>\d{1,2}))?[- ]?";
    $areacode    = "(?:[(\[]?(?<areacode>\d{3})[)\]]?)?[- ]?";
    $body        = "(?:(?<first3>\d{3})[- ]?(?<last4>\d{4}))";  // The only required group
    $ext         = "[- ]?(?:(?:[:;,]|e\w+| )+[ \t]*(?<ext>\d+))?";
    
    $regexp      = "/^" . "{$countrycode}{$areacode}{$body}{$ext}" . "$/";

I went ahead and reinvented the wheel. This function will handle country codes, area codes, and extensions of various formats:

    /**
     * Returns the given phone number in a standardized format.
     * Accepts formatted phone number strings, include area codes, country codes, and extensions.
     *
     * @param string $phone_number
     * @return string
     */
    function format_phone_number(string $phone_number) {
        if (!preg_match('/^(?:\+?(?<countrycode>\d{1,2}))?[- ]?(?:[(\[]?(?<areacode>\d{3})[)\]]?)?[- ]?(?:(?<first3>\d{3})[- ]?(?<last4>\d{4}))[- ]?(?:(?:[:;,]|e\w+| )+[ \t]*(?<ext>\d+))?$/', $phone_number, $parts))
            return preg_replace('/[^0-9]/', '', $phone_number);
        
        $phone = [
            "country_code" => !empty($parts["countrycode"]) ? "+{$parts["countrycode"]}" : null,
            "area_code"    => !empty($parts["areacode"   ]) ? "({$parts["areacode"]})"   : null,
            "body"         => "{$parts["first3"]}-{$parts["last4"]}",
            "extension"    => !empty($parts["ext"        ]) ? "ext. {$parts["ext"]}"     : null,
        ];
    
        return implode(" ", array_filter($phone));
    }

Examples:

    format_phone_number("1231231234 ext 5"    ); // (123) 123-1234 ext. 5
    format_phone_number("123 123 1234:4"      ); // (123) 123-1234 ext. 4
    format_phone_number("123-1231234; 345"    ); // (123) 123-1234 ext. 345
    format_phone_number("123-123-1234"        ); // (123) 123-1234
    format_phone_number("1231231234"          ); // (123) 123-1234
    format_phone_number("1234567"             ); // 123-4567
    format_phone_number("123456789012 43"     ); // +12 (345) 678-9012 ext. 43
    format_phone_number("(123) 456-7890 ext 4"); // (123) 456-7890 ext. 4

Here's a breakdown of the Regular Expression:

    $countrycode = "(?:\+?(?<countrycode>\d{1,2}))?[- ]?";
    $areacode    = "(?:[(\[]?(?<areacode>\d{3})[)\]]?)?[- ]?";
    $body        = "(?:(?<first3>\d{3})[- ]?(?<last4>\d{4}))";  // The only required group
    $ext         = "[- ]?(?:(?:[:;,]|e\w+| )+[ \t]*(?<ext>\d+))?";
    
    $regexp      = "/^" . "{$countrycode}{$areacode}{$body}{$ext}" . "$/";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文