Filter telephone numbers per country code / first digits

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Filter telephone numbers per country code / first digits

Postby Guillaume » Sat Jan 28, 2012 11:41 am

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:

Syntax: [ Download ] [ Hide ]
/^(\+441|\+442|\+447|\+4430|\+4433|\+44500|\+4480|\+336|\+331|\+332|\+333|\+334|\+335|\+33800|\+339)/i


And here's the code that generates it:

Syntax: [ Download ] [ Hide ]
$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.
Last edited by Guillaume on Sat Jan 28, 2012 3:02 pm, edited 1 time in total.
Guillaume
Forum Newbie
 
Posts: 3
Joined: Sat Jan 28, 2012 11:22 am
Location: London, UK

Re: Filter telephone numbers per country code / first digits

Postby ragax » Sat Jan 28, 2012 2:52 pm

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).
:)
User avatar
ragax
Forum Commoner
 
Posts: 85
Joined: Thu Dec 15, 2011 2:40 pm
Location: Nelson, NZ

Re: Filter telephone numbers per country code / first digits

Postby Guillaume » Sat Jan 28, 2012 3:00 pm

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.
Guillaume
Forum Newbie
 
Posts: 3
Joined: Sat Jan 28, 2012 11:22 am
Location: London, UK

Re: Filter telephone numbers per country code / first digits

Postby ragax » Sat Jan 28, 2012 3:05 pm

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.
User avatar
ragax
Forum Commoner
 
Posts: 85
Joined: Thu Dec 15, 2011 2:40 pm
Location: Nelson, NZ

Re: Filter telephone numbers per country code / first digits

Postby Guillaume » Fri Feb 03, 2012 1:58 pm

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
Guillaume
Forum Newbie
 
Posts: 3
Joined: Sat Jan 28, 2012 11:22 am
Location: London, UK

Re: Filter telephone numbers per country code / first digits

Postby ragax » Fri Feb 03, 2012 3:29 pm

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:
Syntax: [ Download ] [ Hide ]
$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!
User avatar
ragax
Forum Commoner
 
Posts: 85
Joined: Thu Dec 15, 2011 2:40 pm
Location: Nelson, NZ


Return to Regex

Who is online

Users browsing this forum: Google [Bot] and 1 guest