How to remove ddd from a number

Asked

Viewed 4,587 times

3

For example, I have a phone number saved in a variable:

$numero = 5198765432

However, now I need to remove the ddd from it (51) to save it in another variable. How could I do this?

  • 2

    $numero will always have this fixed size?

  • Yes @Rafaelalmeida!

  • Now I was in doubt, do you just need to remove the DDD, or do you need it to put in another variable? Or do you need a type formatting 51**98765432?

  • Is that I will use this to save in the database in separate fields, numero_client and ddd_client! @Papacharlie

5 answers

10


One of the possible solutions is to use substr.

$numero = 5198765432;
$ddd_cliente = substr($numero, 0, 2);
$numero_cliente = substr($numero, 2);

echo "DDD: {$ddd_cliente} Número: {$numero_cliente}";

DEMO

Another way using the preg_replace():

$numero = 5198765432;
$ddd_cliente = preg_replace('/\A.{2}?\K[\d]+/', '', $numero);
$numero_cliente = preg_replace('/^\d{2}/', '', $numero);

echo "DDD: {$ddd_cliente} Número: {$numero_cliente}";

DEMO

  • Thank you! Simpler than I imagined :p

  • 1

    so would be right? if (strlen($telefone_cliente) > 9 ) {
 $telefone_sem_ddd = substr($telefone_cliente,2);
 $ddd_cliente = substr($telefone_cliente,0,2);
 }
 else {
 $telefone_sem_ddd = $telefone_cliente;
 $ddd_cliente = '00';
 }

  • 1

    @GWER Apparently right yes. =)

4

$numero = 5198765432;
$ddd = substr($numero, 0, 2);
$numero = substr($numero, 2);

var_dump($ddd);
var_dump($numero);
  • 1

    Thank you for the reply!

3

Introducing another approach, you can work the string as an array (faster than substr) and recover the first 2 characters, so you have the DDD and phone in separate variables.

$phone = '5198765432';
$ddd   = $phone[0] . $phone[1];
$phone = substr( $phone , 2 );

// output: 51 ** 98765432
echo $ddd . '**' . $phone;
  • 1

    Thank you so much for the tip, super useful!

3

It has several forms. One possibility would be to substate

$numero = substr($numero, 2);

So it takes the first two characters from the string

  • 1

    Thank you for the reply!

0

If your input has a mask and the format is (00) 0000-0000 or (00) 00000-0000, you can use REGEX:

/**
* Função auxiliar para extrair DDD e telefone de uma string
*
* (string) telefone (00) 0000-0000 ou (00) 00000-0000
* (string) retorno 'ddd' ou qualquer outra coisa para retornar o telefone
*/
function dddTel(telefone, retorno) {
  var ddd;
  // Remove todos os caractéres que não são números
  telefone = telefone.replace(/\D/g, '');
  // (00) 0000-0000 agora é 0000000000, etc
  if (telefone.length === 10) {
    // Telefone fixo
    ddd = telefone.substr(0, 2);
    telefone = telefone.substr(2, 8);
  } else if (telefone.length === 11) {
    // Telefone celular
    ddd = telefone.substr(0, 2);
    telefone = telefone.substr(2, 9);
  } else {
    // Formato não suportado
    console.log('dddTel() falhou. O telefone informado está num formato não suportado.');
  }
  if (retorno == 'ddd') {
    return ddd;
  } else {
    return telefone;
  }
}

// Uso:
var telefone = (31) 9123-45678;
ddd = dddTel(telefone, 'ddd'); // 31
tel = dddTel(telefone); // 912345678

Browser other questions tagged

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