PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 24, 2014 2:59 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sat Jan 28, 2012 11:41 am 
Offline
Forum Newbie

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

Top
 Profile  
 
PostPosted: Sat Jan 28, 2012 2:52 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Greetings,

Code:
echo preg_match ($c2cregexp ,'+447xxx')

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


Top
 Profile  
 
PostPosted: Sat Jan 28, 2012 3:00 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Sat Jan 28, 2012 3:05 pm 
Offline
Forum Commoner
User avatar

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


Top
 Profile  
 
PostPosted: Fri Feb 03, 2012 1:58 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Fri Feb 03, 2012 3:29 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group