Recursive yaklaşım:
function iterate($words) {
if(($total = count($words)) > 0) {
$str = '';
for($i = 0; $i < $total; $i++ ) {
$str .= ' ' . $words[$i];
echo $str . PHP_EOL;
}
array_shift($words);
iterate($words);
}
}
$text = "Today is a great day.";
$words = str_word_count($text, 1);
iterate($words);
Yukarıda sadece kelimeleri dikkate alacaktır. Bu çiftleri kaldırmaz. Sayılar sözler değildir ve noktalama ya da değil. Beş kelimelik verilen test cümle ile, özyinelemeli yaklaşım daha hızlı array_splice çözüm daha göz ardı edilebilecek bir performans sergiliyor. Bununla birlikte, bu her bir kelimenin ile önemli ölçüde artar. Bir on kelime cümle ile benim makinede hızlı bir kriter neredeyse yarısı sürede bitmiş.
Disclaimer: Isolated Benchmarks depend on a number of factors and may produce different results on different machines. If anything, they can give an indicator about code performance (often in the realms of micro-optimzations), but nothing more. sup>