Skip to content

Instantly share code, notes, and snippets.

@MarceauKa
Created August 11, 2017 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarceauKa/c6e55374c1c847f0d12a6a06050c16ec to your computer and use it in GitHub Desktop.
Save MarceauKa/c6e55374c1c847f0d12a6a06050c16ec to your computer and use it in GitHub Desktop.
Validation RegEx téléphone en PHP
<?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