Jump to content

Checking for the presence of one or more number sequences


Yesideez

Recommended Posts

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.