Question

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';
Was it helpful?

Solution

#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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top