Skip to content

Instantly share code, notes, and snippets.

View ph7jack's full-sized avatar
🏠
Working from home

Pedro Oliveira ph7jack

🏠
Working from home
View GitHub Profile
@ph7jack
ph7jack / formatar_cnpj_cpf.md
Created November 14, 2019 17:12 — forked from davidalves1/formatar_cnpj_cpf.md
Função para formatar CNPJ e CPF, disponível em PHP e JS

PHP

function formatCnpjCpf($value)
{
  $cnpj_cpf = preg_replace("/\D/", '', $value);
  
  if (strlen($cnpj_cpf) === 11) {
    return preg_replace("/(\d{3})(\d{3})(\d{3})(\d{2})/", "\$1.\$2.\$3-\$4", $cnpj_cpf);
  }