Skip to content

Instantly share code, notes, and snippets.

@chrisveness chrisveness/initials.php
Last active Aug 29, 2015

Embed
What would you like to do?
Return initial letters of words
<?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
You can’t perform that action at this time.