Here is something I have used ...
<?php
function Check_phone ($Phone = "",$Format=true)
{
// srtip all but numbers
$Phone = preg_replace ('/[^0-9]/i',"", $Phone);
if(empty($Phone))
{
return array(false,"no Phone number was submitted.");
}
$TheLength = strlen($Phone);
if ($TheLength < 10)
{
return array(false,"phone number is ".$TheLength." digits which is ".(10 - $TheLength)." digits too short in ".$Phone.".");
}
if($TheLength > 10)
{
return array(false,"phone number is ".$TheLength." digits which is ".($TheLength - 10)." digits too long in ".$Phone.".");
}
if($Format){
// creates properly formated number
$FormatedPhone = "(".substr($Phone, 0, 3).") ";
$FormatedPhone .= substr($Phone, 3, 3)."-";
$FormatedPhone .= substr($Phone, 6, 4);
return array(true,$FormatedPhone);
}
return array(true,$Phone);
}
$MyPhoneNumber = "360 384-1616";
list($PhoneOK,$Feedback)=Check_phone($MyPhoneNumber);
echo $PhoneOK." = ".$Feedback;
?>