How to converts the first character of each word and all 3 letter words in a string to uppercase in PHP

1 Answer

0 votes
$string = 'php java css c python sql cpp go csharp';

$array = explode(' ', $string);

foreach ($array as $key => $val) {
    if (strlen($val) <= 3) {
        $array[$key] = strtoupper($val); 
    }
    else {
        $array[$key] = ucfirst($val); 
    }
}

$string = implode(' ', $array);

echo $string;





/*
run:

PHP Java CSS C Python SQL CPP GO Csharp

*/

 



answered Jul 11, 2023 by avibootz

Related questions

...