php 如何将 10 位字符串格式化为电话号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2089923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:57:59  来源:igfitidea点击:

How do you format a 10 digit string into a phone number?

phpregexphone-number

提问by dmanexe

I have database records in the form of 10 character long strings, such as 4085551234.

我有10个字符长字符串形式的数据库记录,例如4085551234。

I wish to format these into this format: (408) 555-1234.

我希望将它们格式化为这种格式:(408) 555-1234。

I think this is regex related. I'm new to programming and completely self-taught here, so any sort of resource relating to performing text processing would be appreciated as well. Thanks!

我认为这是正则表达式相关的。我是编程新手,在这里完全自学,因此任何与执行文本处理相关的资源也将不胜感激。谢谢!

回答by SoapBox

A regex is definitely overkill for this one. If you wanted to take a "phone number" and normalize it to 10 digits, that would be a good use for a regex. To do what you're asking, just do something like:

对于这个,正则表达式绝对是矫枉过正。如果您想获取一个“电话号码”并将其规范化为 10 位数字,那么这对于正则表达式来说将是一个很好的用途。要执行您的要求,只需执行以下操作:

echo '('.substr($data, 0, 3).') '.substr($data, 3, 3).'-'.substr($data,6);

Since you already know how to divide up your data, you can just use substr or something similar to grab the parts you want. RegEx is useful for matching strings which don't always have a strict format. (Like variable numbers of spaces, variable stuff before or after it, extra dashes, etc). But in your case the input is always strictly formatted 10 digits, nothing else, so you don't need the extra overhead of a RegEx to format it.

由于您已经知道如何划分数据,您可以使用 substr 或类似的东西来获取您想要的部分。RegEx 对于匹配并不总是具有严格格式的字符串很有用。(比如可变数量的空格、前后可变的东西、额外的破折号等)。但是在您的情况下,输入始终严格格式化为 10 位数字,没有别的,因此您不需要 RegEx 的额外开销来对其进行格式化。

回答by Rubens Farias

Take a look here: Format phone number

看这里:格式化电话号码

function format_phone($phone)
{
    $phone = preg_replace("/^\d/", "", $phone);

    if(strlen($phone) == 7)
        return preg_replace("/(\d{3})(\d{4})/", "-", $phone);
    elseif(strlen($phone) == 10)
        return preg_replace("/(\d{3})(\d{3})(\d{4})/", "() -", $phone);
    else
        return $phone;
}

回答by Grumdrig

I'd probably go with

我可能会去

$num = "4085551234";  // given                                                  
$formatted = "(".substr($num,0,3).") ".substr($num,3,3)."-".substr($num,6);

Regex isn't really appropriate here.

正则表达式在这里不太合适。

回答by John Weldon

Trivially you could do something like:

简单地说,您可以执行以下操作:

\(\d\{3\}\)\(\d\{3\}\)\(\d\{4\}\)

To match the 10 digits into 3 subgroup expressions, and then print them out using each subgroup:

要将 10 位数字匹配成 3 个子组表达式,然后使用每个子组将它们打印出来:

"() -

But in practice free form data is usually a little trickier

但在实践中,自由形式的数据通常有点棘手

回答by Giselle

I had to do this question for my advanced placement computer science class. Java:

我必须为我的高级计算机科学课做这个问题。爪哇:

Write a program that accepts a 10 digit # and formats it as a phone number.

Ex: 705726552
Output: (705)726-2552

编写一个接受 10 位数字的程序,并将其格式化为电话号码。

例如:705726552
输出:(705)726-2552

import java.util.Scanner;
  public class  TelNumCorrection{

  public static void main(String[]args){

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter a 10 digit number");
    String num=scan.nextLine();

    String a=num.substring(0,3);
    String b=num.substring(3,6);
    String c=num.substring(6);
    System.out.println("("+a+ ")"+b+"-"+c);
  }
}