Generate word combinations
Found from php.net shuffle doc
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];
}
$out_val = implode(" ", $out);
if (count($out) >= $minLength) {
$return[] = $out_val;
}
}
return $return;
}
print_r(powerSet($str_arr));
and the results will be,
Array
(
[0] => windows
[1] => microsoft
[2] => microsoft windows
[3] => new
[4] => new windows
[5] => new microsoft
[6] => new microsoft windows
[7] => buy
[8] => buy windows
[9] => buy microsoft
[10] => buy microsoft windows
[11] => buy new
[12] => buy new windows
[13] => buy new microsoft
[14] => buy new microsoft windows
)
You can have a look at this PEAR PACKAGE Example usage:
<?php
require_once 'Math/Combinatorics.php';
$words = array('buy', 'new', 'microsoft');
$combinatorics = new Math_Combinatorics;
foreach($combinatorics->permutations($words, 2) as $p) {
echo join(' ', $p), "\n";
}
The output will be:
buy new
new buy
buy microsoft
microsoft buy
new microsoft
microsoft new
function pc_permute($items, $perms = array( )) { if (empty($items)) { print join(' ', $perms) . "\n"; } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); pc_permute($newitems, $newperms); } } }
source: http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm#phpckbk-CHP-4-EX-6
also check next example on that site
example: pc_permute( explode( ' ', 'buy new microsoft windows' ) );
buy new microsoft windows new buy microsoft windows buy microsoft new windows microsoft buy new windows new microsoft buy windows microsoft new buy windows buy new windows microsoft new buy windows microsoft buy windows new microsoft windows buy new microsoft new windows buy microsoft windows new buy microsoft buy microsoft windows new microsoft buy windows new buy windows microsoft new windows buy microsoft new microsoft windows buy new windows microsoft buy new new microsoft windows buy microsoft new windows buy new windows microsoft buy windows new microsoft buy microsoft windows new buy windows microsoft new buy
shuffle($array);
echo join(' ', array_slice($array, 0, mt_rand(1, count($array))));
This gives you one random "sentence". Repeat as necessary.