views:

14

answers:

1

Hi all, i'm newer related to Regexp on PHP...

I need to create an Regexp to filter Portuguese Mobile Phones, can anybody help me and explain how i do it? (To i understand it)

Rules:

The integer/string must have 9chars;
The first char must be a '9';
The second one can be '1,2,3,6' (Chars are separeted by commas);
The other chars must be in range of '0-9';

Thanks in advance

+3  A: 
#9[1236][0-9]{7}#

That should do it ;)

Explanation:

# <-- delimiter
    9 <-- 9
    [1236] <-- either of the chars in the brackets
    [0-9]{7} <-- 0-9  7 times
# <-- delimiter

Use: If you want to check whether something is a valid phone number, use:

$isValid = preg_match('#^9[1236][0-9]{7}$#', $phoneNumber);

Notice the ^ and $. These ensure there is only the phone number and nothing more.

nikic
thank you for the explaination, i was trying with / and not #, what's the diference?
CuSS
It would have worked with `/`, too. I simply mostly use `#` or `~` as delimiters, because they occure less often within the regular expression itself and therefore you need to do less escaping.
nikic