Get first letter of each word


//method b
$words = preg_split("/\s+/", "Cheap prescription eyeglasses");
$acronym = "";

foreach ($words as $w) {
$acronym .= $w[0];
}
echo $acronym;

//method c

$words = preg_split("/[\s,_-]+/", "Cheap prescription eyeglasses");
$acronym = "";

foreach ($words as $w) {
$acronym .= $w[0];
}
echo $acronym;

//method d

$language = 'Cheap prescription eyeglasses';

$language = explode(" ", $language);

foreach ($language as $value) {
echo $firstLetter = $value[0];
}

//method e

$string = "Cheap prescription eyeglasses";

function initials($str) {
$ret = '';
foreach (explode(' ', $str) as $word)
$ret .= strtoupper($word[0]);
return $ret;
}

echo initials($string);

//method f
function createAcronym($string) {
$output = null;
$token = strtok($string, ' ');
while ($token !== false) {
$output .= $token[0];
$token = strtok(' ');
}
return $output;
}
$string = 'Cheap prescription eyeglasses';
echo createAcronym($string, false);

//method g

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 = 'Cheap prescription eyeglasses';
echo createAcronym($string);

//method h

function acronym( $string = '' ) {
$words = explode(' ', $string);
if ( ! $words ) {
return false;
}
$result = '';
foreach ( $words as $word ) $result .= $word[0];
return strtoupper( $result );
}

echo acronym( $string = 'Cheap prescription eyeglasses' );

//method i
$string = "Cheap prescription eyeglasses";
$strs=explode(" ",$string);

foreach($strs as $str)
echo $str[0];

//method j

$string = 'Cheap prescription eyeglasses';
$words = explode(' ', $string); // array of word
foreach($words as $word){
echo $word[0]; // first letter
}

//method k

$str = 'Cheap prescription eyeglasses';
echo implode('', array_map(function($v) { return $v[0]; }, explode(' ', $str)));

//method l

// initialize variables
$string = 'Cheap prescription eyeglasses';
$myCapitalizedString = '';

// here's the code
$strs=explode(" ",$string);
foreach($strs as $str) {
$myCapitalizedString .= $str[0];
}

// output
echo $myCapitalizedString; // prints 'Cpe'

*/
//method m

$string = "Cheap prescription eyeglasses";
$arr = explode(" ",$string);

foreach($arr as $s)
{
echo substr($s,0,1);
}

?>