Page 1 of 1

Filter telephone numbers per country code / first digits

Posted: Sat Jan 28, 2012 10:41 am
by Guillaume
Hello,

I am setting up a Click2Call app for my new website. I am trying to keep only numbers that start with specific digits (defined in an array) so that I don't end up being pranked and calling premium numbers in Asia or anywhere else.

Here is my regex:

Code: Select all

/^(\+441|\+442|\+447|\+4430|\+4433|\+44500|\+4480|\+336|\+331|\+332|\+333|\+334|\+335|\+33800|\+339)/i
And here's the code that generates it:

Code: Select all

$authorized_numbers = array("+441","+442","+447","+4430","+4433","+44500","+4480","+336","+331","+332","+333","+334","+335","+33800","+339");
$c2cregexp =  "/^(" . str_replace("+","\+",implode ( '|', $authorized_numbers )). ")/i";
print_r(preg_match ($c2cregexp ,  $_GET['num'] ), $other)
$_GET['num'] was set to +447xxx (xxx where actual digits), then +447, with no match.

This is not my final code obviously, only the first step to test my regex against sample numbers. The whole thing doesn't seem to working so far (result is "0") and a bit of help would be appreciated.

Thank you in advance :)
G.

Re: Filter telephone numbers per country code / first digits

Posted: Sat Jan 28, 2012 1:52 pm
by ragax
Greetings,

Code: Select all

echo preg_match ($c2cregexp ,'+447xxx')
displays "1",
so I would debug $_GET['num'] (taking a close look at what's inside).
:)

Re: Filter telephone numbers per country code / first digits

Posted: Sat Jan 28, 2012 2:00 pm
by Guillaume
Oh thanks! The "+" sign doesn't appear to be in the $_GET['num'] var in the end... and obviously the regex didn't recognise the number without it.

Stupid error, thanks a lot for your help!

G.

Re: Filter telephone numbers per country code / first digits

Posted: Sat Jan 28, 2012 2:05 pm
by ragax
It's a pleasure, glad you got it sorted.
Minor point: you don't seem to need the /i (case insensitive) at the end of $c2cregexp since you're only matching numbers.
Wishing you a fun weekend.

Re: Filter telephone numbers per country code / first digits

Posted: Fri Feb 03, 2012 12:58 pm
by Guillaume
Hi playful,

Sorry for the late reply I have been quite busy since my last post. Thanks for the "/i" tip, I removed it.

I've written quite a lot more code for my Click2Call application, including country detection for international numbers etc, but I am now having another problem with regular expressions. I want to check whether:
- the number starts with specific digits (generated from a "whitelist" array as per my previous posts),
- and then contains only numeric characters,
- the whole string being between x1 and x2 digits, let's say between 10 and 11. Here is what I tried, with no success so far:

My test number: 0620653245 (length: 10)

/^((06|01|02|03|04|05|0800|09)[0-9]+)/ Pass - But doesn't check the whole chain's length

/^((06|01|02|03|04|05|0800|09)[0-9]+){10,11}/ Fails!

/^((06|01|02|03|04|05|0800|09)[0-9]+){3,}/ Fails!

Any idea of what I did wrong this time?

Thanks in advance!

Guillaume

Re: Filter telephone numbers per country code / first digits

Posted: Fri Feb 03, 2012 2:29 pm
by ragax
No problem, you're almost there.

You just need to add a lookahead at the beginning of the string to check the length. Try this:

Code: Select all

$regex=',^(?=\d{10,11}$)((06|01|02|03|04|05|0800|09)[0-9]+),';
To add more conditions, add more lookaheads!
See the "password validation" section at the very top of my regex lookaround page---this is the same technique. If you have more conditions to add, you will love it.

Wishing you a fun weekend!