Skip to content

Instantly share code, notes, and snippets.

@chrisveness
Last active August 29, 2015 14:02
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 chrisveness/6801ef27756b613366f7 to your computer and use it in GitHub Desktop.
Save chrisveness/6801ef27756b613366f7 to your computer and use it in GitHub Desktop.
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