Skip to content

Instantly share code, notes, and snippets.

@marcelod
Forked from wilcorrea/initials.php
Created July 3, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelod/e5dad925278d0fbde91032e8d893bb12 to your computer and use it in GitHub Desktop.
Save marcelod/e5dad925278d0fbde91032e8d893bb12 to your computer and use it in GitHub Desktop.
<?php
/**
* @param string $string
* @param array $avoid (['de', 'da', 'a', 'e', 'o'])
* @return string
*/
function initials(string $string, array $avoid = null): string
{
$words = preg_split("/\s+/", $string);
$restrict = $avoid ? $avoid : ['de', 'da', 'a', 'e', 'o'];
$input = array_diff($words, $restrict);
$function = function ($carry, $item) {
$carry[] = ((string)$item){0};
return $carry;
};
return implode('', array_reduce($input, $function, []))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment