PHP: how to create an regexp to preg_match() for PT Mobile phones?

回眸只為那壹抹淺笑 提交于 2019-12-13 03:02:31

问题


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';

回答1:


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



来源:https://stackoverflow.com/questions/3259334/php-how-to-create-an-regexp-to-preg-match-for-pt-mobile-phones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!