Skip to content

Instantly share code, notes, and snippets.

@bran921007
Created May 28, 2015 08:59
Show Gist options
  • Select an option

  • Save bran921007/a264c3cd8dd3698e7ad6 to your computer and use it in GitHub Desktop.

Select an option

Save bran921007/a264c3cd8dd3698e7ad6 to your computer and use it in GitHub Desktop.
Validar numero de telefono en muchos formatos y modificarlos segun un formato especifico
The Expression
^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$
This regular expression will match any of the following U.S. numbers:
555 555 5555
(555) 555-5555
(555)555-5555
555-555-5555
555.555.5555
5555555555
0-000-000-0000
0.000.000.0000
0 000 000 0000
00000000000
+0-000-000-0000
+0.000.000.0000
+0 000 000 0000
+00000000000
public function formatPhone($phone)
{
if ( preg_match('/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/', $phone, $matches)) {
$matches[1] = ($matches[1]=='') ? '+1' : $matches[1];
return $matches[1].$matches[2].$matches[3].$matches[4];
}
else {
return return Response::json(array(
'success'=>'false',
'msg' =>'Invalid number format'
));
// echo $tel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment