字符串的进步文字组合组合、字符串、文字

由网友(柠の萌°)分享简介:我需要获得一个字符串的进步文字组合。I need to obtain a progressive word combination of a string.例如。 这是字符串输出:这是字符串这是此字符串是字符串本是串E.g. "this is string"Output: "this is string""t...

我需要获得一个字符串的进步文字组合。

I need to obtain a progressive word combination of a string.

例如。 这是字符串 输出:这是字符串         这是         此字符串         是字符串         本         是         串

E.g. "this is string" Output: "this is string" "this is" "this string" "is string" "this" "is" "string"

你知道类似的算法? (我需要它在PHP语言) 感谢;)

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

推荐答案

这是一个简单的code解决您的问题。 我串联每个字符串recoursively到阵列中的其余的

This is a simple code solution to your problem. I concatenate each string recoursively to the remaining ones 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;
}
阅读全文

相关推荐

最新文章