Get the first letter of each word in a string
explode()
on the spaces, then you use an appropriate substring method to access the first character of each word.
$words = explode(" ", "Community College District");
$acronym = "";
foreach ($words as $w) {
$acronym .= mb_substr($w, 0, 1);
}
If you have an expectation that multiple spaces may separate words, switch instead to preg_split()
$words = preg_split("/\s+/", "Community College District");
Or if characters other than whitespace delimit words (-,_
) for example, use preg_split()
as well:
// Delimit by multiple spaces, hyphen, underscore, comma
$words = preg_split("/[\s,_-]+/", "Community College District");
The best way to accomplish this is with regular expressions.
Lets break down what you want in a logical way: You want every character from the string is at the beginning of a word. The best way to identify those characters is to look for those characters that are preceded by white space.
So we start with a lookbehind for that space character, followed by any character:
/(?<=\s)./
This will find any character preceded by a space. But - the first character in the string is a character in the string is one you want extract. And because it's the first character in the string, it can't be preceded by a space. So we want to match anything preceded by a space or the first character in the string, so we add a start-of-subject assertion:
/(?<=\s|^)./
Now we are getting closer. But what if the string contains blocks of multiple spaces? What if it contains a space followed by a punctuation character? We probably don't want to match any of those, in fat we probably just want to match letters. We can do that with "any word character" \w
escape sequence. And we can make are expression case-insensitive using the i
modifier as well as u
modifier to support utf-8 characters.
So we end up with:
/(?<=\s|^)\w/iu
But how do we actually use this in PHP? Well we want to match all occurrences of the regular expression within the string so we use (you guessed it) preg_match_all()
:
$string = "Progress in Veterinary Science";
$expr = '/(?<=\s|^)\w/iu';
preg_match_all($expr, $string, $matches);
Now we have all the characters we wanted to extract. To construct the result string you show, we need to join them together again:
$result = implode('', $matches[0]);
...and we need to ensure that they are all upper-case:
$result = mb_strtoupper($result);
And that's really all there is to it.
See it working
Here's a slightly compressed version, using the alternative regex from Leigh's comment to "capture the initial letters of words separated by hyphens, full stops, etc." (rather than only spaces.)
$str="Foo Bar";
preg_match_all('/(?<=\b)\w/iu',$str,$matches);
$result=mb_strtoupper(implode('',$matches[0]));
Assuming the words are all split by spaces, this is a suitable solution:
$string = "Progress in Veterinary Science";
function initials($str) {
$ret = '';
foreach (explode(' ', $str) as $word)
$ret .= strtoupper($word[0]);
return $ret;
}
echo initials($string); // would output "PIVS"