Formatting Phone Numbers in PHP

前端 未结 20 2134
面向向阳花
面向向阳花 2020-11-28 19:39

I am working on an SMS app and need to be able to convert the sender\'s phone number from +11234567890 to 123-456-7890 so it can be compared to re

相关标签:
20条回答
  • 2020-11-28 19:57

    Check Phone number

    $phone = '+385 99 1234 1234'
    
    $str = preg_match('/^\+?\d{1,3}[\s-]?\d{1,3}[\s-]?\d{1,4}[\s-]?\d{1,4}$/', $phone);
    
    if($str){
    return true;
    }else{
    return false;
    }
    
    0 讨论(0)
  • 2020-11-28 19:58

    Assuming that your phone numbers always have this exact format, you can use this snippet:

    $from = "+11234567890";
    $to = sprintf("%s-%s-%s",
                  substr($from, 2, 3),
                  substr($from, 5, 3),
                  substr($from, 8));
    
    0 讨论(0)
  • 2020-11-28 20:00

    Try something like:

    preg_replace('/\d{3}/', '$0-', str_replace('.', null, trim($number)), 2);
    

    this would take a $number of 8881112222 and convert to 888-111-2222. Hope this helps.

    0 讨论(0)
  • 2020-11-28 20:02

    This is a US phone formatter that works on more versions of numbers than any of the current answers.

    $numbers = explode("\n", '(111) 222-3333
    ((111) 222-3333
    1112223333
    111 222-3333
    111-222-3333
    (111)2223333
    +11234567890
        1-8002353551
        123-456-7890   -Hello!
    +1 - 1234567890 
    ');
    
    
    foreach($numbers as $number)
    {
        print preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
    }
    

    And here is a breakdown of the regex:

    Cell: +1 999-(555 0001)
    
    .*          zero or more of anything "Cell: +1 "
    (\d{3})     three digits "999"
    [^\d]{0,7}  zero or up to 7 of something not a digit "-("
    (\d{3})     three digits "555"
    [^\d]{0,7}  zero or up to 7 of something not a digit " "
    (\d{4})     four digits "0001"
    .*          zero or more of anything ")"
    

    Updated: March 11, 2015 to use {0,7} instead of {,7}

    0 讨论(0)
  • 2020-11-28 20:06

    Here’s my take:

    $phone='+11234567890';
    $parts=sscanf($phone,'%2c%3c%3c%4c');
    print "$parts[1]-$parts[2]-$parts[3]";
    
    //  123-456-7890
    

    The sscanf function takes as a second parameter a format string telling it how to interpret the characters from the first string. In this case, it means 2 characters (%2c), 3 characters, 3 characters, 4 characters.

    Normally the sscanf function would also include variables to capture the extracted data. If not, the data is return in an array which I have called $parts.

    The print statement outputs the interpolated string. $part[0] is ignored.

    I have used a similar function to format Australian phone numbers.

    Note that from the perspective of storing the phone number:

    • phone numbers are strings
    • stored data should not include formatting, such as spaces or hyphens
    0 讨论(0)
  • 2020-11-28 20:08

    I know the OP is requesting a 123-456-7890 format, but, based on John Dul's answer, I modified it to return the phone number in parentheses format, e.g. (123) 456-7890. This one only handles 7 and 10 digit numbers.

    function format_phone_string( $raw_number ) {
    
        // remove everything but numbers
        $raw_number = preg_replace( '/\D/', '', $raw_number );
    
        // split each number into an array
        $arr_number = str_split($raw_number);
    
        // add a dummy value to the beginning of the array
        array_unshift( $arr_number, 'dummy' );
    
        // remove the dummy value so now the array keys start at 1
        unset($arr_number[0]);
    
        // get the number of numbers in the number
        $num_number = count($arr_number);
    
        // loop through each number backward starting at the end
        for ( $x = $num_number; $x >= 0; $x-- ) {
    
            if ( $x === $num_number - 4 ) {
                // before the fourth to last number
    
                $phone_number = "-" . $phone_number;
            }
            else if ( $x === $num_number - 7 && $num_number > 7 ) {
                // before the seventh to last number
                // and only if the number is more than 7 digits
    
                $phone_number = ") " . $phone_number;
            }
            else if ( $x === $num_number - 10 ) {
                // before the tenth to last number
    
                $phone_number = "(" . $phone_number;
            }
    
            // concatenate each number (possibly with modifications) back on
            $phone_number = $arr_number[$x] . $phone_number;
        }
    
        return $phone_number;
    }
    
    0 讨论(0)
提交回复
热议问题