function split_string_into_3_word_lines($str) {
$words = explode(" ", $str);
$threeWordLines = array();
$length = count($words);
for ($i = 0; $i < $length; $i += 3) {
$oneline = "";
for ($j = $i; $j < min($i + 3, $length); $j++) {
$oneline .= $words[$j] . " ";
}
$threeWordLines[] = trim($oneline);
}
return $threeWordLines;
}
$str = "java c c++ python rust go php typescript";
$threeWordLines = split_string_into_3_word_lines($str);
foreach ($threeWordLines as $line) {
echo $line . "\n";
}
/*
run:
java c c++
python rust go
php typescript
*/