Skip to content

Instantly share code, notes, and snippets.

@Slauta
Created December 5, 2013 21:31
Show Gist options
  • Save Slauta/7814272 to your computer and use it in GitHub Desktop.
Save Slauta/7814272 to your computer and use it in GitHub Desktop.
Приведение телефонов Беларуси к единому международному формату
<?php
define('COUNTRY_CODE', '375');
define('COUNTRY_REPLACE_CODE', '80');
define('GRODNO_CODE', '152');
define('GRODNO_PHONE_LENGTH', 6);
$phone = array(
'414243' => '+375152414243',
'#414243' => '+375152414243',
'80291234567' => '+375291234567',
'375291234567' => '+375291234567',
'+375291234567' => '+375291234567',
'8 (029) 123-45-67' => '+375291234567',
'291234567' => '+375291234567',
'1234567' => '+375291234567',
);
$operator_phone_lenght = 7;
$operator_codes = array(
'29',
'33',
'44',
'25',
);
foreach($phone as $phone => $etalon){
$dump = $phone;
$phone = preg_replace('/[^0-9]/u', '', $phone);
$phone = preg_replace('/^('.COUNTRY_REPLACE_CODE.')/u', COUNTRY_CODE, $phone);
if(strlen($phone) == GRODNO_PHONE_LENGTH){
$phone = COUNTRY_CODE . GRODNO_CODE . $phone;
}elseif(strlen($phone) == $operator_phone_lenght){
foreach($operator_codes as $code=>$num){
// foreach((is_array($num) ? $num : array()) as $num_mask){
// if($phone{0} == $num_mask){
// $phone = $code . $phone;
// break;
// }
// }
$phone = COUNTRY_CODE . $num . $phone;
break;
}
}elseif(preg_match("/^(". implode('|',array_values($operator_codes)) .")([0-9]{".$operator_phone_lenght."})/", $phone)){
$phone = COUNTRY_CODE . $phone;
}
if('+'.$phone == $etalon){
echo '+'.$phone . ' => true => '. $dump .' <br>';
}else{
echo '+'.$phone . ' => false => '. $dump .' <br>';
}
}
@Slauta
Copy link
Author

Slauta commented Dec 6, 2013

Нужно учесть варианты:
0291234567
0152234567
152234567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment