views:

96

answers:

8

Hi I'm looking for a way that I can extract the first letter of each word from an input field and place it into a variable.

Example: if the input field is "Stack-Overflow Questions Tags Users" then the output for the variable should be something like "SOQTU"

Any help is appreciated. Thanks

+1  A: 

Something like:

$s = 'Stack-Overflow Questions Tags Users';

if(preg_match_all('/\b(\w)/',strtoupper($s),$m)) {
    $v = implode('',$m[1]); // $v is now SOQTU
}

I'm using the regex \b(\w) to match the word-char immediately following the word boundary.

EDIT: To ensure all your Acronym char are uppercase, you can use strtoupper as shown.

codaddict
Can this be modified to convert small text to capitals?
dmschenk
is this related to the current question or a different question?
codaddict
this is related to your code in this question. your code produces a small letter if that happens to be what the first letter of a word is. I'm just asking how I would convert those to capital letters... I guess it would be a different question.
dmschenk
I've updated my code.
codaddict
Thanks for your help! I wish i was further along with php... I might have realized the answer was staring me right in the face. Thanks again
dmschenk
+1  A: 

If they are separated by only space and not other things. This is how you can do it:

function acronym($longname)
{
    $letters=array();
    $words=explode(' ', $longname);
    foreach($words as $word)
    {
        $word = (substr($word, 0, 1));
        array_push($letters, $word);
    }
    $shortname = strtoupper(implode($letters));
    return $shortname;
}
shamittomar
+1  A: 

Regular expression matching as codaddict says above, or str_word_count() with 1 as the second parameter, which returns an array of found words. See the examples in the manual. Then you can get the first letter of each word any way you like, including substr($word, 0, 1)

Fanis
+3  A: 
$initialism = preg_replace('/\b(\w)\w*\W*/', '\1', $string);
John Kugelman
+1  A: 

The str_word_count() function might do what you are looking for:

$words = str_word_count ('Stack-Overflow Questions Tags Users', 1);
$result = "";
for ($i = 0; $i < count($words); ++$i)
  $result .= $words[$i][0];
dark_charlie
+1 for being different, and using str_word_count. Pity you can't define '-' as a word separator
Mark Baker
A: 
function initialism($str, $as_space = array('-'))
{
    $str = str_replace($as_space, ' ', trim($str));
    $ret = '';
    foreach (explode(' ', $str) as $word) {
        $ret .= strtoupper($word[0]);
    }
    return $ret;
}

$phrase = 'Stack-Overflow Questions IT Tags Users Meta Example';
echo initialism($phrase);
// SOQITTUME
GZipp
This is pretty cool, it also had some benifits I hadn't initially thought of like converting small text to caps and the ability to easily control the separator markers used ('-','/',',')etc...
dmschenk
And all that without having to spend your otherwise-productive hours dithering with regular expressions.
GZipp
GZipp
A: 

Just to be completely different:

$input = 'Stack-Overflow Questions Tags Users';

$acronym = implode('',array_diff_assoc(str_split(ucwords($input)),str_split(strtolower($input))));

echo $acronym;
Mark Baker
interesting that it picks up caps in the same word. that could certainly be useful.
dmschenk
That's a "side-effect" of the ucwords() function... makes it useful for capitalising double-barelled names
Mark Baker
+2  A: 
$s = 'Stack-Overflow Questions Tags Users';
echo preg_replace('~\b(\w)|.~', '$1', $s);

the same as codaddict's but shorter

stereofrog
+1 That's impressively clever.
John Kugelman