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


Get first letters of substrings in string

问题可能不清楚,但这是我想要实现的。

我有以下字符串:

$input  = 'foo_bar_buz_oof_rab';

我需要得到以下字符串的输出:

$output = 'fbbor';

正如您所看到的,重点是用_分解字符串,并获得子字符串的第一个字母。最好的方法是什么?Regex,分解并循环子字符串?

$words = explode("_", "BLA_BLA_BLA_BLA");
$acronym = "";
foreach ($words as $w) {
  $acronym .= $w[0];
}

你是这个意思?

您可以使用和substr函数来实现此

像低于

$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;

这将提供您想要的输出

我也用:

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