Yesideez Posted June 4, 2012 Share Posted June 4, 2012 I've got a site where users can make posts and I need to check for the presence of phone numbers. Phone numbers can either be 01xxxxxxxxx or 07xxxxxxxxx depending on land line or mobile (total of 11 digits). I think the easiest way to check is to strip out any non-numeric character (can this be done with regex?) which will bring all numbers together, then check with a much simpler regex. I need an if() to check for the presence of either number and return true or false. Big problem is, I've tried to learn this (wasted many hours) and can't get my head around it. Any help would be gratefully appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/263624-checking-for-the-presence-of-one-or-more-number-sequences/ Share on other sites More sharing options...
Yesideez Posted June 4, 2012 Author Share Posted June 4, 2012 Just remembered - some land line numbers in smaller villages are still only 5 digits so where mobile numbers will always be 11 digits, land line (01) numbers can be either 10 or 11 digits. 01xxxxxxxx or 01xxxxxxxxx or 07xxxxxxxxx Quote Link to comment https://forums.phpfreaks.com/topic/263624-checking-for-the-presence-of-one-or-more-number-sequences/#findComment-1351047 Share on other sites More sharing options...
joe92 Posted June 11, 2012 Share Posted June 11, 2012 Is this what you mean? <?php $testInput = 'Hello, here is my number, 01911222333, or is it 0175522114, or even the mobile, 07520111444...'; /*the preliminary check to see if there is a phone number*/ if(preg_match("/0(1\d{8,9}|7\d{9})/", $testInput)){ /*now match the phone numbers*/ preg_match_all("/0(1\d{8,9}|7\d{9})/", $testInput, $phoneNumberMatches); /*do with it what you will*/ print_r($phoneNumberMatches[0]); } Here it is in action: http://codepad.viper-7.com/OVa6jq Quote Link to comment https://forums.phpfreaks.com/topic/263624-checking-for-the-presence-of-one-or-more-number-sequences/#findComment-1352883 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.