Formatting Phone Numbers in PHP

前端 未结 20 2131
面向向阳花
面向向阳花 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:43

    It's faster than RegEx.

    $input = "0987654321"; 
    
    $output = substr($input, -10, -7) . "-" . substr($input, -7, -4) . "-" . substr($input, -4); 
    echo $output;
    
    0 讨论(0)
  • 2020-11-28 19:46

    Please have a look at substr based function that can change formats

    function phone(string $in): string
    {
        $FORMAT_PHONE = [1,3,3,4];
        $result =[];
        $position = 0;
        foreach ($FORMAT_PHONE as $key => $item){
            $result[] = substr($in, $position, $item);
            $position += $item;
        }
        return '+'.implode('-',$result);
    }
    
    0 讨论(0)
  • 2020-11-28 19:47
    $data = '+11234567890';
    
    if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data,  $matches ) )
    {
        $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
        return $result;
    }
    
    0 讨论(0)
  • 2020-11-28 19:47

    Phone numbers are hard. For a more robust, international solution, I would recommend this well-maintained PHP port of Google's libphonenumber library.

    Using it like this,

    use libphonenumber\NumberParseException;
    use libphonenumber\PhoneNumber;
    use libphonenumber\PhoneNumberFormat;
    use libphonenumber\PhoneNumberUtil;
    
    $phoneUtil = PhoneNumberUtil::getInstance();
    
    $numberString = "+12123456789";
    
    try {
        $numberPrototype = $phoneUtil->parse($numberString, "US");
    
        echo "Input: " .          $numberString . "\n";
        echo "isValid: " .       ($phoneUtil->isValidNumber($numberPrototype) ? "true" : "false") . "\n";
        echo "E164: " .           $phoneUtil->format($numberPrototype, PhoneNumberFormat::E164) . "\n";
        echo "National: " .       $phoneUtil->format($numberPrototype, PhoneNumberFormat::NATIONAL) . "\n";
        echo "International: " .  $phoneUtil->format($numberPrototype, PhoneNumberFormat::INTERNATIONAL) . "\n";
    } catch (NumberParseException $e) {
        // handle any errors
    }
    

    you will get the following output:

    Input: +12123456789
    isValid: true
    E164: +12123456789
    National: (212) 345-6789
    International: +1 212-345-6789
    

    I'd recommend using the E164 format for duplicate checks. You could also check whether the number is a actually mobile number or not (using PhoneNumberUtil::getNumberType()), or whether it's even a US number (using PhoneNumberUtil::getRegionCodeForNumber()).

    As a bonus, the library can handle pretty much any input. If you, for instance, choose to run 1-800-JETBLUE through the code above, you will get

    Input: 1-800-JETBLUE
    isValid: true
    E164: +18005382583
    National: (800) 538-2583
    International: +1 800-538-2583
    

    Neato.

    It works just as nicely for countries other than the US. Just use another ISO country code in the parse() argument.

    0 讨论(0)
  • 2020-11-28 19:48

    This is for UK landlines without the Country Code

    function format_phone_number($number) {
        $result = preg_replace('~.*(\d{2})[^\d]{0,7}(\d{4})[^\d]{0,7}(\d{4}).*~', '$1 $2 $3', $number);
        return $result;
    }
    

    Result:

    2012345678
    becomes
    20 1234 5678
    
    0 讨论(0)
  • 2020-11-28 19:49

    Don't reinvent the wheel! Import this amazing library:
    https://github.com/giggsey/libphonenumber-for-php

    $defaultCountry = 'SE'; // Based on the country of the user
    $phoneUtil = PhoneNumberUtil::getInstance();
    $swissNumberProto = $phoneUtil->parse($phoneNumber, $defaultCountry);
    
    return $phoneUtil->format($swissNumberProto, PhoneNumberFormat::INTERNATIONAL);
    

    It is based on Google's library for parsing, formatting, and validating international phone numbers: https://github.com/google/libphonenumber

    0 讨论(0)
提交回复
热议问题