Jump to content

[SOLVED] phone matching U.S. format


2levelsabove

Recommended Posts

Hello why is this not working ? all i want is to be able to match numbers in format of

 

xxx-xxx-xxxx  or (xxx) xxx-xxxx

 

 

 


$phone_number = 'sjkadkjsahsahdsjkjdjhdshkjdk jsakhkjhk 469-767-7695 sadsadsadsadsad';



$pattern3='/^([0-1]([\s-./\\])?)?(\(?[2-9]\d{2}\)?|[2-9]\d{3})([\s-./\\])?(\d{3}([\s-./\\])?\d{4}|[a-zA-Z0-9]{7})$/';

if (preg_match($pattern3, $phone_number, $matches)) 
{
    // we have a match, dump sub-patterns to $matches
    $phone_number = $matches[0]; // original number
    $area_code = $matches[1];    // 3-digit area code
    $exchange = $matches[2];     // 3-digit exchange
    $number = $matches[3];       // 4-digit number
    $extension = $matches[4];    // extension


print_r($matches);

}

Link to comment
Share on other sites

I have used contionals:

 

$str = '(123) 345-6789';
if(preg_match('#^(\()?\d{3}(?(1)\)\x20|-)\d{3}-\d{4}$#', $str, $match)){
echo 'The number format is good...';
} else {
echo 'The number format has errors...';
}

 

You can alter $str to not have brackets (but you will then need to add a dash as in the xxx-xxx-xxxx example in the OP.

If you only remove one bracket, you should have an error regardless to whether there is a space or dash after the first 3 digits..

if both brackets are there, then there must be a space following the area code's closing bracket (as in (xxx) xxx-xxxx in the OP).

 

So the basic breakdown is this:

 

^(\))?  from the start, find a '(' character as optional

\d{3}   then find three consecutive characters

(?(1)\)\x20|-)   this is the contional part.. if there is a capture from the first set of parethesis, then look for the closing ')' character, followed by a space, otherwise, look for a dash

\d{3}-\d{4}$  then, finally look for 3 digits, a dash then 4 digits.

 

Link to comment
Share on other sites

Daniel,

 

ok i tried this. but still nothing is outputted.

 

$phone_number = 'sjkadkjsahsahdsjkjdjhdshkjdk jsakhkjhk 469-767-7695 sadsadsadsadsad';



$pattern3="/^(?:\((\d{3})\) |(\d{3})-)(\d{3})-(\d{4})$/";

if (preg_match_all($pattern3, $phone_number, $matches)) 
{
    // we have a match, dump sub-patterns to $matches
    $phone_number = $matches[0]; // original number
    $area_code = $matches[1];    // 3-digit area code
    $exchange = $matches[2];     // 3-digit exchange
    $number = $matches[3];       // 4-digit number
    $extension = $matches[4];    // extension


print_r($matches);

}

Link to comment
Share on other sites

Oops.. I was so fixated on the xxx-xxx-xxxx or (xxx) xxx-xxxx part I forgot that it was in a string with other characters in it.. (in my example, $str contains only the phone number).. should have included the sample sentence in the OP..so yeah, remove the ^ and $ characters from the pattern. My mistake  :-[

 

Link to comment
Share on other sites

Hold the phone guys... now that I test my pattern against:

$str = 'sjkadkjsahsahdsjkjdjhdshkjdk jsakhkjhk 469-767-7695 sadsadsadsadsad';

 

My regex doesn't hold I don't think (not when I start playing with brackets surrounding the area code).. I'll revise my pattern.

 

 

Link to comment
Share on other sites

ok.. here is a quick revised version.. this version assumes only word characters surround the phone number:

 

$str = 'sjkadkjsahsahdsjkjdjhdshkjdk jsakhkjhk (469) 767-7695 sadsadsadsadsad';
if(preg_match('#(?:\w+)?(\()?\d{3}(?(1)\)\x20|-)\d{3}-\d{4}(?:\w+)?#', $str, $match)){
echo 'The number format is good...';
} else {
echo 'The number format has errors...';
}

 

So I added (?:\w+)? at each end of the patter.. this does not capture if it does find your typical [a-zA-Z0-9_] characters one or more times (all of these optional of course).

Running this preg expression against the updated $str seems to hold up.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.