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($regex, '($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?