Progressive string word combination

I need to get a progressive combination of words from a string.

eg. "this string" Result: "this string" "this" "this string" "string" "this" "is" "String"

Do you know a similar algorithm? (I need this in php language) Thanks;)

+3
source share
2 answers

This is a simple solution to your problem. I concatenate each row repeatedly to the remaining in the array.

$string = "this is a string";  
$strings = explode(' ', $string);

// print result
print_r(concat($strings, ""));

// delivers result as array
function concat(array $array, $base_string) {

    $results = array();
    $count = count($array);
    $b = 0;
    foreach ($array as $key => $elem){
        $new_string = $base_string . " " . $elem;
        $results[] = $new_string;
        $new_array = $array;

        unset($new_array[$key]);

        $results = array_merge($results, concat ($new_array, $new_string));

    }
    return $results;
}
+4
source
+1

Source: https://habr.com/ru/post/1793573/


All Articles