简体   繁体   English

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

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

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! 谢谢!

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. 如果你想拿一个“电话号码”并将其标准化为10位数,这对正则表达式来说是一个很好的用法。 To do what you're asking, just do something like: 为了做你要问的事,做一些像:

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. 由于您已经知道如何划分数据,因此您可以使用substr或类似的东西来获取所需的部分。 RegEx is useful for matching strings which don't always have a strict format. RegEx对于匹配并不总是具有严格格式的字符串很有用。 (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. 但在您的情况下,输入始终严格格式化为10位数字,没有别的,所以您不需要RegEx的额外开销来格式化它。

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})/", "$1-$2", $phone);
    elseif(strlen($phone) == 10)
        return preg_replace("/(\d{3})(\d{3})(\d{4})/", "($1) $2-$3", $phone);
    else
        return $phone;
}

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. 正则表达式在这里并不合适。

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个子组表达式,然后使用每个子组打印出来:

"(\1) \2-\3

But in practice free form data is usually a little trickier 但实际上,自由格式数据通常有点棘手

I had to do this question for my advanced placement computer science class. 我不得不为我的高级计算机科学课做这个问题。 Java: Java的:

Write a program that accepts a 10 digit # and formats it as a phone number. 编写一个接受10位数#的程序并将其格式化为电话号码。

Ex: 705726552 例如: 705726552
Output: (705)726-2552 输出:( (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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM