开发者

Industrial Phone Number Regex (a Regex problem)

I'm attempting to extract phone numbers from messages submitted to me via a contact form. This "contact form" gets over a thousand submissions a day, and a lot of these submitters are making formatting mistakes, such as:

"Please call me back at925-943-2343 ext. 304" (no space before number)

Currently, my regex, which is below, is missing these numbers with certain formatting errors (such as the lack of space before the number)

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($r开发者_开发技巧egex, '($1) $2-$3 ext. $4', $msg); 
        echo $phonenumber;
    }
}

Any tips?

Related issue:

$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;
}

Why does this return nothing?


Try this:

[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}(?:i.{0,3}x?.{0,9})(\d{1,5})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜