Last active
August 29, 2015 14:02
-
-
Save chrisveness/6801ef27756b613366f7 to your computer and use it in GitHub Desktop.
Return initial letters of words
This file contains 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 | |
/** | |
* Returns initial letters of all words from all arguments. | |
* | |
* @param $string $str,... | |
* @return string | |
* @example initials('Johann Sebastian', 'Bach') => 'JSB'. | |
*/ | |
function initials() | |
{ | |
// qv stackoverflow.com/questions/9706429 | |
$args = implode(' ', func_get_args()); | |
// use lookbehind to find all Unicode letters preceded by word boundary | |
preg_match_all('/(?<=\b)\p{L}/', $args, $matches); | |
$initials = implode('', $matches[0]); | |
return strtoupper($initials); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment