PHP phone number parser

前端 未结 4 1025
忘了有多久
忘了有多久 2021-01-20 18:27

Building an application for UK & Ireland only but potentially it might extend to other countries. We have built an API and I\'m trying to decided how A) to store phone n

4条回答
  •  礼貌的吻别
    2021-01-20 19:09

    ?php
    $array = array
    (
            '0871231234',
            '087 123 1234',
            '087-1231234',
            '+353871231234'
    );
    
    foreach($array as $a)
            if(preg_match("/(^[0-9]{10}$)|(^[0-9]{3}\ [0-9]{3}\ [0-9]{4}$)|(^[0-9]{3}\-[0-9]{7}$)|(^\+353[0-9]{9}$)/", $a))
            {
                    // removes +35
                    $a = preg_replace("/^\+[0-9]{2}/", '', $a);
                    // removes first number
                    $a = preg_replace("/^[0-9]{1}/", '', $a);
                    // removes spaces and -
                    $a = preg_replace("/(\s+)|(\-)/", '', $a);
                    $a = "00353".$a;
                    echo $a."\n";
            }
    ?>
    

提交回复
热议问题