0

How to generate multiple string base on given string with space separator. Ex.: I have one string like

Ex.:1 'This is string' will output following strings: 'This is', 'This string', 'is string'

$arrStrSplit = explode($splitBy, $strInfo);
for($outer = 0; $outer < count($arrStrSplit); $outer++)
{
    for($inner = $outer+1; $inner < count($arrStrSplit); $inner++)
    {
        array_push($arrStrSplitPair, $arrStrSplit[$outer].' '.$arrStrSplit[$inner]);
    }
}

What if have multiple word string more than 3+

Ex.2: 'This is new string'

  Output:

  'This is', 'This new', 'This string', 'is new', 'is string', 'new string',
  'This is new', 'This is string', 'This new string', 'is new string'

so on : Input string have any no. of words. From 2 to 15-20 etc...

i.e. No. of word string from 2 TO (No. of word of given string - 1)

lowerends
  • 5,469
  • 2
  • 24
  • 36
Jignesh
  • 117
  • 1
  • 10
  • possible duplicate of [PHP: How to get all possible combinations of 1D array?](http://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array). All You must do is explode(' ', $string) to get an array :). – Jacek Kowalewski Jul 26 '14 at 08:39
  • In this also generate duplicate sting.. Like, 'Alpha Beta' and 'Alpha Beta' i.e. combination of word should be unique. Flow from left to right. – Jignesh Jul 26 '14 at 08:43

1 Answers1

1

After many updates...

http://php.net/manual/en/function.shuffle.php#88408

$string = 'This is new string';
$array = explode(' ', $string);

function powerSet($in, $minLength = 1) {
    $count = count($in);
    $members = pow(2,$count);
    $return = array();
    for ($i = 0; $i < $members; $i++) {
        $b = sprintf("%0".$count."b",$i);
        $out = array();
        for ($j = 0; $j < $count; $j++) {
            if ($b{$j} == '1') $out[] = $in[$j];
        }
        if (count($out) >= $minLength) {
            $return[] = $out;
        }
    }
    return $return;
} 
$sets = powerSet($array, 1);

That's all I think! Best regards!

Jacek Kowalewski
  • 2,761
  • 2
  • 23
  • 36