3v4l.org

run code in 300+ PHP versions simultaneously
<?php function splort($org, $stop_words) { $words = explode(' ', $org); $abbrevs = array(''); foreach ($words as $word) { if (in_array($word, $stop_words)) { // create a new set of abbreviations with this word's initial included $new_abbrevs = array(); foreach ($abbrevs as $abbrev) { $new_abbrevs[] = $abbrev . $word[0]; } // merge the arrays $abbrevs = array_merge($abbrevs, $new_abbrevs); } else { // add the initial to each abbreviation foreach ($abbrevs as &$abb) { $abb .= $word[0]; } } } return $abbrevs; } $stop_words = array('of', 'and'); print_r(splort('State Bank of and India', $stop_words));
Output for 7.1.25 - 7.1.31, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Array ( [0] => SBI [1] => SBoI [2] => SBaI [3] => SBoaI )

preferences:
187.55 ms | 403 KiB | 173 Q