note 122458 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Sun, 03 Apr 2022 20:41:56 +0000 |
| Subject: | note 122458 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218000@lists.php.net to get a copy of this message | ||
Note Submitter: alves dot david at outlook dot com
----
// Function to format Brazilian taxvat using preg_replace
// Função para formatar o CPF ou CPF utilizando preg_replace
if (!function_exists('cpf_cnpj')) {
function cpf_cnpj($cpf_cnpj)
{
if (!in_array(strlen($cpf_cnpj), [11, 14])) {
return $cpf_cnpj;
}
if (strlen($cpf_cnpj) == 11) {
return preg_replace("/(\d{3})(\d{3})(\d{3})(\d{2})/", "$1.$2.$3-$4",
$cpf_cnpj);
} else {
return preg_replace("/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/",
"$1.$2.$3/$4-$5", $cpf_cnpj);
}
}
}
echo cpf_cnpj(12345678901), ' - ', cpf_cnpj(12345678000190);
// 123.456.789-01 - 12.345.678/0001-90