Created
August 11, 2015 10:38
-
-
Save ridvanbaluyos/ec339aea3be07e29be65 to your computer and use it in GitHub Desktop.
Regex for Philippine Numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((\+[0-9]{2})|0)[.\- ]?9[0-9]{2}[.\- ]?[0-9]{3}[.\- ]?[0-9]{4} | |
or | |
((\+63)|0)[.\- ]?9[0-9]{2}[.\- ]?[0-9]{3}[.\- ]?[0-9]{4} | |
will match these: | |
+63.917.123.4567 | |
+63-917-123-4567 | |
+63 917 123 4567 | |
+639171234567 | |
09171234567 |
it will work.
<?php
$pattern = "/((\+[0-9]{2})|0)[.\- ]?9[0-9]{2}[.\- ]?[0-9]{3}[.\- ]?[0-9]{4}/";
$numbers = [
// Valid
'+63.917.123.4567',
'+63-917-123-4567' ,
'+63 917 123 4567',
'+639171234567',
'09171234567',
// Invalid
'aaa',
'1234567890',
'0000000000',
'0917123456A',
'',
null,
];
foreach ($numbers as $number) {
$isValid = preg_match($pattern, $number) ? 'valid' : 'invalid';
echo $number . ": {$isValid} \n";
}
Will output:
+63.917.123.4567: valid
+63-917-123-4567: valid
+63 917 123 4567: valid
+639171234567: valid
09171234567: valid
aaa: invalid
1234567890: invalid
0000000000: invalid
0917123456A: invalid
: invalid
: invalid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sir not working. I tried to insert this to HTML pattern