在 PHP 中去除电话号码的括号、空格和连字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5109538/
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
Stripping a phone number of its parenthesis, spaces, and hyphens in PHP?
提问by AKor
I have a phone number stored in $phone
, it looks like this: (555) 555-5555
. I want it to look like this: 5555555555
. How do I take the string and strip it of hyphens, spaces, and parenthesis?
我有一个电话号码存储在 中$phone
,它看起来像这样:(555) 555-5555
。我希望它看起来是这样的:5555555555
。如何获取字符串并去掉连字符、空格和括号?
回答by outis
With a regexp. Specifically, use the preg_replace
function:
用正则表达式。具体来说,使用preg_replace
函数:
$phone = preg_replace('/\D+/', '', $phone);
回答by Paulraj
preg_replace("/[^0-9]/", "", $phone);
回答by mario
Cumbersome method for regex avoiders:
正则表达式避免者的繁琐方法:
implode(array_filter(str_split('(555) 555-5555'), 'is_numeric'));
回答by Emma
Method 1
方法一
Another option is to simply use the preg_match_all
with a simple expression:
另一种选择是简单地使用preg_match_all
带有简单表达式的 :
[0-9]+
Test
测试
$phone_number_inputs = ['(555) 555-5555', '(444) 444 4444', '333-333-3333', '1-222-222-2222', '1 (888) 888-8888', '1 (777) 777-7777',
'11111(555) 555-5555'];
$phone_number_outputs = [];
foreach ($phone_number_inputs as $str) {
preg_match_all('/[0-9]+/', $str, $numbers, PREG_SET_ORDER, 0);
foreach ($numbers as $number) {
$phone_number .= $number[0];
}
if (strlen($phone_number) == 10 || strlen($phone_number) == 11) {
array_push($phone_number_outputs, $phone_number);
print(" " . $phone_number . " may be a phone number! \n");
} else {
print(" " . $phone_number . " may not be a phone number! \n");
}
$phone_number = null;
}
var_export($phone_number_outputs);
Output
输出
5555555555 may be a phone number!
4444444444 may be a phone number!
3333333333 may be a phone number!
12222222222 may be a phone number!
18888888888 may be a phone number!
17777777777 may be a phone number!
111115555555555 may not be a phone number!
and var_export($phone_number_outputs);
would print:
并var_export($phone_number_outputs);
会打印:
array (
0 => '5555555555',
1 => '4444444444',
2 => '3333333333',
3 => '12222222222',
4 => '18888888888',
5 => '17777777777',
)
Method 2
方法二
It would be best to find solutions similar to method 1.
最好找到类似于方法 1 的解决方案。
However, we can also design some more complicated expressions depending on the inputs that we might have, such as with:
但是,我们也可以根据我们可能拥有的输入设计一些更复杂的表达式,例如:
^(?:[0-9][- ])?\(?[0-9]{3}\)?[- ][0-9]{3}[- ][0-9]{4}$
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
如果你想简化/修改/探索表达式,它已经在regex101.com 的右上角面板中进行了解释。如果您愿意,您还可以在此链接中观看它如何与某些示例输入匹配。
RegEx Circuit
正则表达式电路
jex.imvisualizes regular expressions:
jex.im可视化正则表达式: