PHP: Come creare un regexp a preg_match () per i telefoni cellulari PT?
-
16-09-2020 - |
Domanda
Sono più nuovo correlato a Regexp su Php ...
Devo creare un Regexp per filtrare i telefoni cellulari portoghesi, qualcuno può aiutarmi e spiegare come lo faccio?(A capirlo)
Regole:
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';
. Soluzione
#9[1236][0-9]{7}#
.
dovrebbe farlo;)
Spiegazione:
# <-- delimiter
9 <-- 9
[1236] <-- either of the chars in the brackets
[0-9]{7} <-- 0-9 7 times
# <-- delimiter
.
Uso: se vuoi controllare se qualcosa è un numero di telefono valido, utilizzare:
$isValid = preg_match('#^9[1236][0-9]{7}$#', $phoneNumber);
.
Si noti il ^
e $
.Questi assicurano che vi sia solo il numero di telefono e niente di più.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow