- array_unshift: documentation ( source)
- preg_match_all: documentation ( source)
- var_export: documentation ( source)
<?php
$string = 'hello world good morning';
$result = [];
$i = 0;
while (preg_match_all("/(?=\b(\w+(?: \w+){{$i}})\b)/", $string, $m)) {
array_unshift($result, $m[1]);
++$i;
}
var_export($result);