2levelsabove Posted September 14, 2008 Share Posted September 14, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/ Share on other sites More sharing options...
Daniel0 Posted September 14, 2008 Share Posted September 14, 2008 Try this: ^(?:\((\d{3})\) |(\d{3})-)(\d{3})-(\d{4})$ It'll match both 123-456-7890 and (123) 456-7890 Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641026 Share on other sites More sharing options...
nrg_alpha Posted September 14, 2008 Share Posted September 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641117 Share on other sites More sharing options...
2levelsabove Posted September 14, 2008 Author Share Posted September 14, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641151 Share on other sites More sharing options...
Daniel0 Posted September 14, 2008 Share Posted September 14, 2008 Well, nrg_alpha's pattern is better because it won't result in empty backreferences. Also, the ^ and $ means "match start of string" and "match end of string", respectively, so you cannot have that. You could remove those meta-characters. Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641167 Share on other sites More sharing options...
nrg_alpha Posted September 14, 2008 Share Posted September 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641170 Share on other sites More sharing options...
2levelsabove Posted September 14, 2008 Author Share Posted September 14, 2008 Thanks a lot guys !!! you guys are awesome!! Im just so new with regex! Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641173 Share on other sites More sharing options...
nrg_alpha Posted September 14, 2008 Share Posted September 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641175 Share on other sites More sharing options...
nrg_alpha Posted September 14, 2008 Share Posted September 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641177 Share on other sites More sharing options...
2levelsabove Posted September 14, 2008 Author Share Posted September 14, 2008 Thanks alpha. I appreciate your help on this matter Quote Link to comment https://forums.phpfreaks.com/topic/124147-solved-phone-matching-us-format/#findComment-641289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.