Skip to content

Instantly share code, notes, and snippets.

@Slauta
Created December 5, 2013 21:31

Revisions

  1. Slauta renamed this gist Dec 5, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Slauta created this gist Dec 5, 2013.
    60 changes: 60 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?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>';
    }

    }