在 PHP 中为美国电话号码添加破折号的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5872096/
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
Function to add dashes to US phone number in PHP
提问by Catfish
What is the best way to add dashes to a phone number in PHP? I have a number in the format xxxxxxxxxx
and I want it to be in the format xxx-xxx-xxxx. This only applies to 10 digit US phone numbers.
在 PHP 中为电话号码添加破折号的最佳方法是什么?我有一个格式的数字,xxxxxxxxxx
我希望它的格式为 xxx-xxx-xxxx。这仅适用于 10 位美国电话号码。
回答by Thilo
$number = "1234567890";
$formatted_number = preg_replace("/^(\d{3})(\d{3})(\d{4})$/", "--", $number);
EDIT: To be a bit more generic and normalize a US phone number given in any of a variety of formats (which should be common practice - there's no reason to force people to type in a phone number in a specific format, since all you're interested in are the digits and you can simply discard the rest):
编辑:为了更通用一些并规范以各种格式中的任何一种给出的美国电话号码(这应该是常见的做法 - 没有理由强迫人们以特定格式输入电话号码,因为所有你对数字感兴趣,您可以简单地丢弃其余部分):
function localize_us_number($phone) {
$numbers_only = preg_replace("/[^\d]/", "", $phone);
return preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "--", $numbers_only);
}
echo localize_us_number("5551234567"), "\n";
echo localize_us_number("15551234567"), "\n";
echo localize_us_number("+15551234567"), "\n";
echo localize_us_number("(555) 123-4567"), "\n";
echo localize_us_number("+1 (555) 123-4567"), "\n";
echo localize_us_number("Phone: 555 1234567 or something"), "\n";
回答by Emil Vikstr?m
$number = '1234567890';
if(ctype_digit($number) && strlen($number) == 10) {
$number = substr($number, 0, 3) .'-'.
substr($number, 3, 3) .'-'.
substr($number, 6);
}
Or if you for some reason want to avoid substr, but this is more resource hungry (not much though - choose the one you prefer):
或者,如果您出于某种原因想要避免使用 substr,但这需要更多资源(尽管不多 - 选择您喜欢的那个):
$number = '1234567890';
if(ctype_digit($number) && strlen($number) == 10) {
$parts = str_split($number, 3);
$number = $parts[0] .'-'. $parts[1] .'-'. $parts[3].$parts[4];
}
回答by nyanev
iterate through the string and make counter. When counter is 3 or 7 insert dash.
遍历字符串并生成计数器。当计数器为 3 或 7 时,插入破折号。
回答by Erik
I feel obliged to post. Cheesiest solution:
我觉得有必要发帖。最简单的解决方案:
$number = "1234567890";
$formatted_number = "$number[0]$number[1]$number[2]-$number[3]$number[4]$number[5]-$number[6]$number[7]$number[8]$number[9]";
But it works and its fast. vs. the preg_replace solution:
但它的工作原理和速度很快。与 preg_replace 解决方案对比:
250,000 iterations:
250,000 次迭代:
preg_replace: 1.23 seconds ugly solution: 0.866 seconds
preg_replace:1.23 秒丑解:0.866 秒
Pretty meaningless but fun :P
很无意义但很有趣:P
回答by Zack Katz
Here's what I used. It's not perfect, but it's an improvement over @Thilo's answer. It checks for a leading 1
. If it's there, it ignores it. The code also ignores separating dashes, commas, and spaces, so it will work with 1231231234
, 123 123 1234
, and 123.123.1234
. It doesn't handle numbers with parenthesis, but I'm sure there's another thread out there with that solution!
这是我使用的。它并不完美,但比@Thilo 的答案有所改进。它检查领先的1
. 如果它在那里,它会忽略它。该代码还忽略了分离破折号,逗号和空格,所以它会与工作1231231234
,123 123 1234
和123.123.1234
。它不处理带括号的数字,但我敢肯定还有另一个线程可以解决该解决方案!
$formatted_number = preg_replace("/^1?(?:[- .])?(\d{3})(?:[- .])?(\d{3})(?:[- .])?(\d{4})$/", "() -", $not_formatted_phone_number);
回答by yak27
A modification of Thilo's answer providing complete conditional formatting control over the leading "1".
Thilo 的答案的修改提供了对前导“1”的完整条件格式控制。
public function phoneFormat($number) {
$numbersOnly = preg_replace("/[^\d]/", "", $number);
$nums = array_filter(explode("-", preg_replace("/^(1|)(\d{3})(\d{3})(\d{4})$/",
"---", $numbersOnly)));
$output = $numbersOnly;
if(count($nums) == 3){
$output = "($nums[1])-$nums[2]-$nums[3]";
}elseif(count($nums) == 4){
$output = "$nums[0]-($nums[1])-$nums[2]-$nums[3]";
}
return $output;
}
回答by AnarchyOutlaw
Here's what I came up with:
这是我想出的:
function format_phone($var_num) {
$var_num = trim($var_num);
$var_num = str_replace("(","",$var_num);
$var_num = str_replace(")","",$var_num);
$var_num = str_replace("-","",$var_num);
$var_num = str_replace(" ","",$var_num);
$var_num = str_replace(".","",$var_num);
$var_num = substr($var_num, -10);
$var_area_code = substr($var_num, 0, -7);
$var_exchange = substr($var_num, 3, -4);
$var_extention = substr($var_num, -4);
$var_return = "{$var_area_code}-{$var_exchange}-{$var_extention}";
return $var_return;
}
// Examples:
$phone_number = "1 (757) 555-1212";
// $phone_number = "17575551212";
// $phone_number = "(757) 555-1212";
// $phone_number = "757.555.1212";
echo "{$phone_number} = " . format_phone($phone_number);