Created
August 11, 2017 09:58
-
-
Save MarceauKa/c6e55374c1c847f0d12a6a06050c16ec to your computer and use it in GitHub Desktop.
Validation RegEx téléphone en PHP
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
<?php | |
$regex = "/^((?:\+?33|0)(?:[6-7]{1})(?:[\-\s\.]{0,}[0-9]{2})+)$/im"; | |
$subjects = [ | |
'0601020304', | |
'+33601020304', | |
'33601020304', | |
'06.01.02.-.03.04', | |
'06-01---02-03-04', | |
'06 01 02 03 04', | |
'06 01 02 03 04', | |
]; | |
foreach ($subjects as $key => $value) { | |
$matches = []; | |
preg_match($regex, $subjects[$key], $matches); | |
if (count($matches)) { | |
$match = $matches[0]; | |
$match = str_replace(['+33'], '0', $match); | |
$number = str_replace(['.', '-', ' '], '', $match); | |
echo $value . ' => ' . $number . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment