Jump to content

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

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.