[英]reg ex telephone number
How do i change any of the following to just numbers 如何将以下任何内容更改为仅数字
1-(999)-999-9999
1-999-999-9999
1-999-9999999
1(999)-999-9999
1(999)999-9999
i want the final product to be 19999999999 我希望最终产品是19999999999
The easiest way would be to strip everything out of your string that's not a number and then see if you end up with a 10 digit number (or 11 if you're making the 1 mandatory): 最简单的方法是从字符串中删除不是数字的所有内容,然后查看是否最终得到一个10位数字(如果你强制要求,则为11):
$string = "1-(999)-999-9999";
$number = preg_replace('/[^0-9]/', "", $string); // results in 19999999999
if (strlen($number) == 11)
{
// Probably have a phone number
}
try 尝试
preg_replace('\D', '', $string);
This will filter out any non digits. 这将过滤掉任何非数字。
你真的不需要正则表达式......
$number = str_replace(array('-', '(', ')'), '', $number);
preg_replace('[\D*]', '', '1-(999)-999-9999');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.