在字符串中获取子串的第一个字母

Question can be unclear, but here is what I want to achieve.

I have following string:

$input  = 'foo_bar_buz_oof_rab';

I need to get in the output following string:

$output = 'fbbor';

As you can see, the point is to explode string with _ and get the first letters of the substrings. What is the best method to get it ? Regex, explode and loop over substrings ?

$words = explode("_", "BLA_BLA_BLA_BLA");
$acronym = "";

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

You mean this?

you can use and substr function to achieve this

like below

$str = "foo_bar_buz_oof_rab";

$arr = explode("_",$str);
$new_str = '';
foreach($arr as $a){

  $new_str .= $new_str .substr($a,0,1);

}

echo $new_str;

This will give your desired output

I also did it with:

$output = implode(array_map(function($k){ return $k[0]; }, explode('_', $input)));