Text.php
public static function phoneFormat($phoneNumber): string
    {
        if (!$phoneNumber) {
            return (string) $phoneNumber;
        }
        $prefix = trim($phoneNumber)[0] == '+';
        $value = preg_replace('/[^0-9]/', '', (string) $phoneNumber);
        $valueLen = mb_strlen($value, self::ENCODING);
        if ($prefix && $valueLen == 11) {
            $pattern[] = '/([0-9][0-9])([0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/';
            $replace[] = '$1 ($2) $3 $4 $5 $6';
        } else if ($prefix && $valueLen == 12 && $value[2] == 0) {
            $pattern[] = '/([0-9][0-9]).([0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/';
            $replace[] = '$1 ($2) $3 $4 $5 $6';
        } elseif ($valueLen == 12) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4 $5';
        } elseif ($valueLen == 11) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4';
        } elseif ($valueLen == 10) {
            $pattern[] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4 $5';
        } elseif ($valueLen == 9) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])$/';
            $replace[] = '$1 $2 $3';
        } elseif ($valueLen == 8) {
            $pattern[] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4';
        } elseif ($valueLen == 7) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3';
        } elseif ($valueLen == 6) {
            $pattern[] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3';
        }
        
        if (!isset($pattern)) {
            return ($prefix ? '+' : '') . $value;
        }
        return ($prefix ? '+' : '') . trim(preg_replace($pattern, $replace, $value));
    }
public static function phoneFormat($phoneNumber): string
    {
        if (!$phoneNumber) {
            return (string) $phoneNumber;
        }
        $prefix = trim($phoneNumber)[0] == '+';
        $value = preg_replace('/[^0-9]/', '', (string) $phoneNumber);
        $valueLen = mb_strlen($value, self::ENCODING);
        if ($prefix && $valueLen == 11) {
            $pattern[] = '/([0-9][0-9])([0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/';
            $replace[] = '$1 ($2) $3 $4 $5 $6';
        } else if ($prefix && $valueLen == 12 && $value[2] == 0) {
            $pattern[] = '/([0-9][0-9]).([0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/';
            $replace[] = '$1 ($2) $3 $4 $5 $6';
        } elseif ($valueLen == 12) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4 $5';
        } elseif ($valueLen == 11) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4';
        } elseif ($valueLen == 10) {
            $pattern[] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4 $5';
        } elseif ($valueLen == 9) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])$/';
            $replace[] = '$1 $2 $3';
        } elseif ($valueLen == 8) {
            $pattern[] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3 $4';
        } elseif ($valueLen == 7) {
            $pattern[] = '/^([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3';
        } elseif ($valueLen == 6) {
            $pattern[] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])$/';
            $replace[] = '$1 $2 $3';
        }
        
        if (!isset($pattern)) {
            return ($prefix ? '+' : '') . $value;
        }
        return ($prefix ? '+' : '') . trim(preg_replace($pattern, $replace, $value));
    }
public static function phoneFormat ( $ phoneNumber ) : string { if ( ! $ phoneNumber ) { return ( string ) $ phoneNumber ; } $ prefix = trim ( $ phoneNumber ) [ 0 ] == '+' ; $ value = preg_replace ( '/[^0-9]/' , '' , ( string ) $ phoneNumber ) ; $ valueLen = mb_strlen ( $ value , self :: ENCODING ) ; if ( $ prefix && $ valueLen == 11 ) { $ pattern [ ] = '/([0-9][0-9])([0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/' ; $ replace [ ] = '$1 ($2) $3 $4 $5 $6' ; } else if ( $ prefix && $ valueLen == 12 && $ value [ 2 ] == 0 ) { $ pattern [ ] = '/([0-9][0-9]).([0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/' ; $ replace [ ] = '$1 ($2) $3 $4 $5 $6' ; } elseif ( $ valueLen == 12 ) { $ pattern [ ] = '/^([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3 $4 $5' ; } elseif ( $ valueLen == 11 ) { $ pattern [ ] = '/^([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3 $4' ; } elseif ( $ valueLen == 10 ) { $ pattern [ ] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3 $4 $5' ; } elseif ( $ valueLen == 9 ) { $ pattern [ ] = '/^([0-9][0-9][0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3' ; } elseif ( $ valueLen == 8 ) { $ pattern [ ] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3 $4' ; } elseif ( $ valueLen == 7 ) { $ pattern [ ] = '/^([0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3' ; } elseif ( $ valueLen == 6 ) { $ pattern [ ] = '/^([0-9][0-9])([0-9][0-9])([0-9][0-9])$/' ; $ replace [ ] = '$1 $2 $3' ; } if ( ! isset ( $ pattern ) ) { return ( $ prefix ? '+' : '' ) . $ value ; } return ( $ prefix ? '+' : '' ) . trim ( preg_replace ( $ pattern , $ replace , $ value ) ) ; }
Format a phone number
@param mixed $phoneNumber
@return string
Format a phone number