Last active
October 19, 2023 14:28
-
-
Save stephandesouza/6cb3c9fae78683a80b504f1cf351932e to your computer and use it in GitHub Desktop.
Encurtador de Nomes (similar a cartões de crédito)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Função que recebe o nome completo de uma pessoa redunzido as partes do meio - maiores que 3 caracteres- a ponto, ou sem, conforme combinado | |
* | |
* Ex.: JOÃO BELTRANO CICLANO DA SILVA > (com ponto) > JOÃO B. C. DA SILVA | |
* Ex.: JOÃO BELTRANO CICLANO DA SILVA > (sem ponto) > JOÃO B C DA SILVA | |
* Ex.: MARIA ANTONIETA > (sem nomes do meio) > MARIA ANTONIETA) | |
* | |
* @param string $nome Nome completo a ser encurtado. | |
* @param bool $ponto Será usado ponto no encurtamento do nome? Padrão: true | |
* | |
* @return string Nome reduzido. | |
*/ | |
function encurtarNome($nome, $ponto = true) | |
{ | |
$partes = explode(' ', $nome); | |
$total = count($partes); | |
if ($total > 2) { | |
$string = []; | |
foreach ($partes as $k => $v) { | |
if ($k == 0 || $k == $total - 1 || strlen($v) < 3) { | |
$string[] = $v; | |
} else { | |
$string[] = $v[0] . ($ponto ? '.' : ''); | |
} | |
} | |
$string = implode(' ', $string); | |
} else { | |
$string = $nome; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
função encurtarNome ( $ nome , $ ponto = true )