-
-
Save marcelod/af512bba3f4841bb77ad849782de0052 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* @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
// para funcionar no php 5.6