Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the first letter of each word in a string

Tags:

string

php

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.

like image 795
Grigor Avatar asked Mar 14 '12 16:03

Grigor


People also ask

How do I get the first letter of a string to a string?

To get the first and last characters of a string, access the string at the first and last indexes. For example, str[0] returns the first character, whereas str[str. length - 1] returns the last character of the string.


6 Answers

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");
like image 107
Michael Berkowski Avatar answered Oct 29 '22 08:10

Michael Berkowski


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]));
like image 31
2 revs, 2 users 85% Avatar answered Oct 29 '22 08:10

2 revs, 2 users 85%


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"
like image 22
casraf Avatar answered Oct 29 '22 08:10

casraf


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);
like image 36
Sverri M. Olsen Avatar answered Oct 29 '22 07:10

Sverri M. Olsen


As explained by others, classical way consist in iterating over each word of your initial string, reduce the word to its first letter, and combine those first letters together.

Here is a helper method combining the different steps.

/**
 * @return string
 */
function getInitials($string = null) {
    return array_reduce(
        explode(' ', $string),
        function ($initials, $word) {
            return sprintf('%s%s', $initials, substr($word, 0, 1));
        },
        ''
    );
}

NB : this will return an empty string in case the given string is empty.

getInitials('Community College District')

string 'CCD' (length=3)

getInitials()

string '' (length=0)

getInitials('Lorem ipsum dolor sic amet')

string 'Lidsa' (length=5)

Of course you can add filters to the callback function of array_reduce(), such as strtoupper() if you prefer only uppercased initials for instance.

like image 10
Flo Schild Avatar answered Oct 29 '22 07:10

Flo Schild


Michael Berkowski's (and others) answer, simplified to one line and working correctly on multi-byte characters (i.e. making abbreviation / initials out of non-Latin string):

foreach(explode(' ', $words) as $word) $acronym .= mb_substr($word, 0, 1, 'utf-8');

Using mb_substr($word, 0, 1, 'utf-8'), instead of $word[0] seems to be must, if you're working on non-Latin, multi-byte strings and characters, i.e. when using UTF-8 encoded strings.

like image 9
trejder Avatar answered Oct 29 '22 07:10

trejder