How to get first letter of each word in a string with PHP

2 Answers

0 votes
$str = "PHP C Python Java Go Rust C#";
 
$words = explode(" ", $str);
 
foreach($words as $word) {
    echo $word[0] . " ";
}
 
 
 
/*
run:
 
P C P J G R C 
 
*/

 



answered Nov 10, 2022 by avibootz
edited Jun 6, 2023 by avibootz
0 votes
$str = "PHP C Python Java Go Rust C#";
  
preg_match_all('/\b\w/', $str, $matches);

$firstLetters = implode(' ', $matches[0]);

echo $firstLetters; 
  
  
  
  
/*
run:
  
P C P J G R C
  
*/

 



answered Dec 10, 2023 by avibootz
...