我正在尝试从通过联系表单提交给我的邮件中提取电话号码。这种“联系表单”每天收到超过一千份提交,许多提交者正在制作格式错误,例如:
“请致电925-943-2343分机304”(号码前没有空格)
目前,我的正则表达式(如下所示)缺少这些数字,但存在某些格式错误(例如数字前缺少空格)
foreach (...)
{
$regex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})'
.'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})'
.'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
if (preg_match($regex, $msg))
{
$phonenumber = preg_replace($regex, '($1) $2-$3 ext. $4', $msg);
echo $phonenumber;
}
}
任何提示?
相关问题:
$regex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})'
.'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})'
.'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
$line = "(732) 912 0159 ";
if (preg_match($regex, $line))
{
$phonenumber = preg_replace($regex, '($1) $2-$3 ext. $4', $line);
echo $phonenumber;
}
为什么没有返回?
答案 0 :(得分:0)
试试这个:
[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}(?:i.{0,3}x?.{0,9})(\d{1,5})