How would I get the first letter of each word for a given string?
$string = "Community College District";
$result = "CCD";
I found the javascript method but wasn't sure how to convert it to php.
To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.
Step 1 : Define one HashMap called charCountMap with Character as key and Integer as value. This map will hold the characters and their count in the given string. Step 2 : Convert inputString to char array called strArray. Step 3 : Iterate through all chars of strArray and update their occurrences in charCountMap.
There are a lot of explode
answers. I think using the strtok
function is a much more elegant and memory-efficient solution:
function createAcronym($string) {
$output = null;
$token = strtok($string, ' ');
while ($token !== false) {
$output .= $token[0];
$token = strtok(' ');
}
return $output;
}
$string = 'Progress in Veterinary Science';
echo createAcronym($string, false);
Here is a more robust and useful function, which supports UTF8 characters and the option to only use the capitalized words:
function createAcronym($string, $onlyCapitals = false) {
$output = null;
$token = strtok($string, ' ');
while ($token !== false) {
$character = mb_substr($token, 0, 1);
if ($onlyCapitals and mb_strtoupper($character) !== $character) {
$token = strtok(' ');
continue;
}
$output .= $character;
$token = strtok(' ');
}
return $output;
}
$string = 'Leiðari í Kliniskum Útbúgvingum';
echo createAcronym($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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With