#!/usr/bin/env php
<?php
function check_phone($s)
{
$n = str_replace( ['-', ' '], '', $s); //spaces and '-' can be put in so many ways, that it's better to ignore
//them, rather than wonder about every possible (and valid) combination
//note ugly two-element array here
if ( preg_match( '/^\\(?\\+?[0-9]{2,3}\\)?[0-9]{7}([0-9]{2})?$/', $n ) == 1 )
{
//country/area code (which is optional) can be given in brackets () and prefixed with +
//follwed by 7 digits (landline), plus 2 optional (mobile)
echo("VALID $s [ aka $n ]\n");
return 1;
} else {
echo("INVALID $s [ aka $n ]\n");
return 0;
}
}
$phone_array = array( '123456789', '(12)9009090', '900-900-900', '+12 900 29 29', 'i900900900', '900 0-00 290', '+900123123', '0+00 900 900', '(+90) 900 200 300', '+490 900 100 200');
foreach ($phone_array as $phone)
{
check_phone($phone);
}
?>