Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get initial characters of a string? [duplicate]

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 881
Grigor Avatar asked Mar 14 '12 16:03

Grigor


People also ask

How do I find duplicate characters in a string?

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.

How do you find the first repeated and non repeated character in a string?

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.


4 Answers

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 27
Sverri M. Olsen Avatar answered Sep 17 '22 11:09

Sverri M. Olsen


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 72
Michael Berkowski Avatar answered Sep 19 '22 11:09

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 22
2 revs, 2 users 85% Avatar answered Sep 17 '22 11:09

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 31
casraf Avatar answered Sep 20 '22 11:09

casraf