Skip to content

Instantly share code, notes, and snippets.

@stephandesouza
Last active October 19, 2023 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephandesouza/6cb3c9fae78683a80b504f1cf351932e to your computer and use it in GitHub Desktop.
Save stephandesouza/6cb3c9fae78683a80b504f1cf351932e to your computer and use it in GitHub Desktop.
Encurtador de Nomes (similar a cartões de crédito)
<?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;
}
@Juanggdr
Copy link

função encurtarNome ( $ nome , $ ponto = true )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment