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); }