Format 9 and 8 digit phone numbers

Asked

Viewed 16,160 times

8

I want to format phone numbers for the following formats:

(XX) XXXX - XXX = 11 2222 3333

But as some new numbers come with DD + 9 digits, for example:

(XX) XXXXX - XXXX = 11 22222 3333

How to format in order to fill the empty spaces with zeroes or by the number of digits and to format automatically?

  • Come from the bank these values? It’s all together? There’s a similar question

  • Yes, they are stored in the bank all together, ex: 1122223333

  • Format in back-end or in a <input> front-end?

  • Back-End, tagged php rs

  • In the example of this question he assumes that everyone starts with 9, but as I will take the value of the user, it may not be exactly that, you understand... I think with something with regex would be good.

6 answers

6

Check the amount of characters in the string if 10 needs to add the ninth digit of the opposite.

<?php
    function formataTelefone($numero){
        if(strlen($numero) == 10){
            $novo = substr_replace($numero, '(', 0, 0);
            $novo = substr_replace($novo, '9', 3, 0);
            $novo = substr_replace($novo, ')', 3, 0);
        }else{
            $novo = substr_replace($numero, '(', 0, 0);
            $novo = substr_replace($novo, ')', 3, 0);
        }
        return $novo;
    }

    $str = '1122223333';
    echo formataTelefone($str);

Exit:

(11)922223333
  • 1

    And how would the formatting be? (XX) ZZZZZ - YYYY

  • @ngthm4r3 ddd is another field in the table?

  • No, it all comes together, 00111112222

  • @NGTHM4R3, something is missing?

  • Sorry, I hadn’t seen that you edited the answer, I’ll test, Thank you!

  • Yeah, any warning. @NGTHM4R3

  • Opa, Thanks for the code, I found this function here and implemented it like this: http://pastebin.com/c0gj92ix Source: http://blog.clares.com.br/php-mascara-cnpj-cpf-data-e-qualquer-other-thing/

  • @NGTHM4R3 good too.

Show 3 more comments

6

I know it’s an old question, but if anyone’s still in need:

protected function formatPhone($phone)
{
    $formatedPhone = preg_replace('/[^0-9]/', '', $phone);
    $matches = [];
    preg_match('/^([0-9]{2})([0-9]{4,5})([0-9]{4})$/', $formatedPhone, $matches);
    if ($matches) {
        return '('.$matches[1].') '.$matches[2].'-'.$matches[3];
    }

    return $phone; // return number without format
}

return $this->formatPhone('11987654321');
return $this->formatPhone('1187654321');

2

 static public function masc_tel($TEL) {
    $tam = strlen(preg_replace("/[^0-9]/", "", $TEL));
      if ($tam == 13) { // COM CÓDIGO DE ÁREA NACIONAL E DO PAIS e 9 dígitos
      return "+".substr($TEL,0,$tam-11)."(".substr($TEL,$tam-11,2).")".substr($TEL,$tam-9,5)."-".substr($TEL,-4);
      }
      if ($tam == 12) { // COM CÓDIGO DE ÁREA NACIONAL E DO PAIS
      return "+".substr($TEL,0,$tam-10)."(".substr($TEL,$tam-10,2).")".substr($TEL,$tam-8,4)."-".substr($TEL,-4);
      }
      if ($tam == 11) { // COM CÓDIGO DE ÁREA NACIONAL e 9 dígitos
      return "(".substr($TEL,0,2).")".substr($TEL,2,5)."-".substr($TEL,7,11);
      }
      if ($tam == 10) { // COM CÓDIGO DE ÁREA NACIONAL
      return "(".substr($TEL,0,2).")".substr($TEL,2,4)."-".substr($TEL,6,10);
      }
      if ($tam <= 9) { // SEM CÓDIGO DE ÁREA
      return substr($TEL,0,$tam-4)."-".substr($TEL,-4);
      }
  }

how to use:

echo self::masc_tel('5512123456789');
  • Clarify your answer, add other details to highlight how you solved it and the form that proposes the solution. If you want more information on how to answer, go to [].

1

function telephone($number){
    $number="(".substr($number,0,2).") ".substr($number,2,-4)." - ".substr($number,-4);
    // primeiro substr pega apenas o DDD e coloca dentro do (), segundo subtr pega os números do 3º até faltar 4, insere o hifem, e o ultimo pega apenas o 4 ultimos digitos
    return $number;
}

I think it’s the easiest way

  • 2

    It worked perfect here! Thank you!

  • 1

    This way it is much simpler and the format is better. It looks like (99) 99999 - 9999 or (99) 9999 - 9999. Thank you!

  • yes, simple and super efficient, valid for mobile or not

0

Function for 9 digits + DDD

function formataTelefone($numero){
   $formata = substr($numero, 0, 2);
   $formata_2 = substr($numero, 3, 5);
   $formata_3 = substr($numero, 4, 4);
   return "(".$formata.") " . $formata_2 . "-". $formata_3;
}

Output format: (xx) xxxxx-xxxx

0

I wear it like this.

    function clean_char($string) {
        return preg_replace('/\D/', '', $string);
    }   

    function telefone_format($tel) {
        $tel = clean_char($tel);
        if(empty($tel)){
            return " Nenhum"; //OPCINAL
        }
        $telcheck = substr($tel, 0, 4);
        if (strpos($telcheck, '0300') || strpos($telcheck, '0800')) {
            preg_match('/(\d{4})(\d{3})(\d{4})/', $tel, $matches);

        //RETORNO 0800 355 1234
        return $matches[1] . " " . $matches[2] . " " . $matches[3];
    }
    if (strpos($tel, '4007')) {
        preg_match('/(\d{4})(\d{4})/', $tel, $matches);

        //RETORNO 4007 1234
        return $matches[1] . "-" . $matches[2];
    }
    $telcheck = substr($tel, 2, 1);
    if (strpos($tel, '9')) {
        preg_match('/(\d{2})(\d{5})(\d{4})/', $tel, $matches);
        return "(" . $matches[1] . ") " . $matches[2] . "-" . $matches[3];
    }
    preg_match('/(\d{2})(\d{4})(\d{4})/', $tel, $matches);
    return "(" . $matches[1] . ") " . $matches[2] . "-" . $matches[3];
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.